From 697b88c15b890f6a4d9aea73345527f07dc7be55 Mon Sep 17 00:00:00 2001 From: jzbor Date: Thu, 29 Oct 2020 23:11:55 +0100 Subject: [PATCH 1/2] No need to keep the configuration in the CWD anymore. Added support for a ~/.config/nameinator/nameserver-globals.csv and a ~/.config/nameinator/domains.txt file. --- loader.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/loader.go b/loader.go index a4d6895..dd200f2 100644 --- a/loader.go +++ b/loader.go @@ -7,6 +7,7 @@ import ( "io" "math/rand" "os" + "os/user" "time" ) @@ -14,7 +15,15 @@ func prepareBenchmarkNameservers(nsStore nsInfoMap) { if appConfiguration.nameserver == "" { // read global nameservers from given file fmt.Println("trying to load nameservers from datasrc/nameserver-globals.csv") - readNameserversFromFile(nsStore, "datasrc/nameserver-globals.csv") // TODO: Split read and Load + // @ TODO refactor - there shouldn't be the same code in if and else + // (but atm this is the only safe way)... + if _, err := os.Stat("datasrc/nameserver-globals.csv"); err == nil { + readNameserversFromFile(nsStore, "datasrc/nameserver-globals.csv") // TODO: Split read and Load + } else if usr, err := user.Current(); err == nil{ + readNameserversFromFile(nsStore, usr.HomeDir + "/.config/nameinator/nameserver-globals.csv") // TODO: Split read and Load + } else { + readNameserversFromFile(nsStore, "datasrc/nameserver-globals.csv") // TODO: Split read and Load + } } else { loadNameserver(nsStore, appConfiguration.nameserver, "givenByParameter") } @@ -24,7 +33,18 @@ func prepareBenchmarkDomains(dStore dInfoMap) { var domains []string // read domains from given file fmt.Println("trying to load domains from datasrc/alexa-top-2000-domains.txt") - alldomains, err := readloadDomainsFromFile("datasrc/alexa-top-2000-domains.txt") + var err error + var alldomains []string + var usr *user.User + // @ TODO refactor - there shouldn't be the same code in if and else + // (but atm this is the only safe way)... + if _, err = os.Stat("datasrc/alexa-top-2000-domains.txt"); err == nil { + alldomains, err = readloadDomainsFromFile("datasrc/alexa-top-2000-domains.txt") + } else if usr, err = user.Current(); err == nil{ + alldomains, err = readloadDomainsFromFile(usr.HomeDir + "/.config/nameinator/domains.txt") + } else { + alldomains, err = readloadDomainsFromFile("datasrc/alexa-top-2000-domains.txt") + } _ = err // TODO: Exception handling in case that the files do not exist // randomize domains from file to avoid cached results rand.Seed(time.Now().UnixNano()) From 19ec5ae280e10f8a895d359ec5fbb4f4f5af3e63 Mon Sep 17 00:00:00 2001 From: jzbor Date: Fri, 30 Oct 2020 01:39:31 +0100 Subject: [PATCH 2/2] Adjusting the output messages according to the previous commit --- loader.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/loader.go b/loader.go index dd200f2..4b5585d 100644 --- a/loader.go +++ b/loader.go @@ -14,14 +14,16 @@ import ( func prepareBenchmarkNameservers(nsStore nsInfoMap) { if appConfiguration.nameserver == "" { // read global nameservers from given file - fmt.Println("trying to load nameservers from datasrc/nameserver-globals.csv") // @ TODO refactor - there shouldn't be the same code in if and else // (but atm this is the only safe way)... if _, err := os.Stat("datasrc/nameserver-globals.csv"); err == nil { + fmt.Println("trying to load nameservers from datasrc/nameserver-globals.csv") readNameserversFromFile(nsStore, "datasrc/nameserver-globals.csv") // TODO: Split read and Load } else if usr, err := user.Current(); err == nil{ + fmt.Println("trying to load nameservers from ~/.config/nameinator/nameserver-globals.csv") readNameserversFromFile(nsStore, usr.HomeDir + "/.config/nameinator/nameserver-globals.csv") // TODO: Split read and Load } else { + fmt.Println("trying to load nameservers from datasrc/nameserver-globals.csv") readNameserversFromFile(nsStore, "datasrc/nameserver-globals.csv") // TODO: Split read and Load } } else { @@ -32,17 +34,19 @@ func prepareBenchmarkNameservers(nsStore nsInfoMap) { func prepareBenchmarkDomains(dStore dInfoMap) { var domains []string // read domains from given file - fmt.Println("trying to load domains from datasrc/alexa-top-2000-domains.txt") var err error var alldomains []string var usr *user.User // @ TODO refactor - there shouldn't be the same code in if and else // (but atm this is the only safe way)... if _, err = os.Stat("datasrc/alexa-top-2000-domains.txt"); err == nil { + fmt.Println("trying to load domains from datasrc/alexa-top-2000-domains.txt") alldomains, err = readloadDomainsFromFile("datasrc/alexa-top-2000-domains.txt") } else if usr, err = user.Current(); err == nil{ + fmt.Println("trying to load domains from ~/.config/nameinator/domains.txt") alldomains, err = readloadDomainsFromFile(usr.HomeDir + "/.config/nameinator/domains.txt") } else { + fmt.Println("trying to load domains from datasrc/alexa-top-2000-domains.txt") alldomains, err = readloadDomainsFromFile("datasrc/alexa-top-2000-domains.txt") } _ = err // TODO: Exception handling in case that the files do not exist