From f6a55213ddcbd6ba73de3572776ab1a33d626ac1 Mon Sep 17 00:00:00 2001 From: Lyndsay Roger Date: Fri, 25 Sep 2015 15:51:21 +1200 Subject: [PATCH] Improve importing json config files --- network.go | 15 +++------------ seeder.go | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/network.go b/network.go index 738a51e..39eaa4f 100644 --- a/network.go +++ b/network.go @@ -87,10 +87,6 @@ func initNetwork(jnw JNetwork) (*dnsseeder, error) { return nil, errors.New(fmt.Sprintf("No DNS Hostname supplied")) } - if _, ok := config.seeders[jnw.Name]; ok { - return nil, errors.New(fmt.Sprintf("Name already exists from previous file - %s", jnw.Name)) - } - // init the seeder seeder := &dnsseeder{} seeder.theList = make(map[string]*node) @@ -130,14 +126,9 @@ func initNetwork(jnw JNetwork) (*dnsseeder, error) { if seeder.ttl < 60 { seeder.ttl = 60 } - // check for duplicates - for _, v := range config.seeders { - if v.id == seeder.id { - return nil, errors.New(fmt.Sprintf("Duplicate Magic id. Already loaded for %s so can not be used for %s", v.id, v.name, seeder.name)) - } - if v.dnsHost == seeder.dnsHost { - return nil, errors.New(fmt.Sprintf("Duplicate DNS names. Already loaded %s for %s so can not be used for %s", v.dnsHost, v.name, seeder.name)) - } + + if dup, err := isDuplicateSeeder(seeder); dup == true { + return nil, err } return seeder, nil diff --git a/seeder.go b/seeder.go index 8fd167f..ac9ce5b 100644 --- a/seeder.go +++ b/seeder.go @@ -1,6 +1,8 @@ package main import ( + "errors" + "fmt" "log" "net" "strconv" @@ -408,6 +410,21 @@ func getSeederByName(name string) *dnsseeder { return nil } +// isDuplicateSeeder returns true if the seeder details already exist in the application +func isDuplicateSeeder(s *dnsseeder) (bool, error) { + + // check for duplicate seeders with the same details + for _, v := range config.seeders { + if v.id == s.id { + return true, errors.New(fmt.Sprintf("Duplicate Magic id. Already loaded for %s so can not be used for %s", v.id, v.name, s.name)) + } + if v.dnsHost == s.dnsHost { + return true, errors.New(fmt.Sprintf("Duplicate DNS names. Already loaded %s for %s so can not be used for %s", v.dnsHost, v.name, s.name)) + } + } + return false, nil +} + /* - */ +*/