diff --git a/CHANGELOG.md b/CHANGELOG.md index fdc247c..6c51ffb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [0.2.1] + +- Fix dataDir flag parsing + + ## [0.2.0] - Switch to go modules (go >= 1.11 required) diff --git a/README.md b/README.md index e6eeea8..6fbe31f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # GoipLookup - geoiplookup for GeoLite2 written in Go +[![Go Report Card](https://goreportcard.com/badge/github.com/axllent/goiplookup)](https://goreportcard.com/report/github.com/axllent/goiplookup) + GoipLookup is a geoiplookup replacement for the [free GeoLite2-Country](https://dev.maxmind.com/geoip/geoip2/geolite2/), written in [Go](https://golang.org/). diff --git a/goiplookup.go b/goiplookup.go index 309d24d..1f40481 100644 --- a/goiplookup.go +++ b/goiplookup.go @@ -15,11 +15,12 @@ var ( showhelp bool verboseoutput bool showversion bool + dataDir string version = "dev" ) // we set this in `main()` based on OS -var dataDir (*string) +// var dataDir (*string) // URLs const ( @@ -31,9 +32,9 @@ const ( func main() { // alternate default path for OSX if runtime.GOOS == "darwin" { - dataDir = flag.String("d", "/usr/local/share/GeoIP", "database directory or file") + flag.StringVarP(&dataDir, "dir", "d", "/usr/local/share/GeoIP", "database directory or file") } else { - dataDir = flag.String("d", "/usr/share/GeoIP", "database directory or file") + flag.StringVarP(&dataDir, "dir", "d", "/usr/share/GeoIP", "database directory or file") } flag.BoolVarP(&country, "country", "c", false, "return country name") @@ -85,10 +86,10 @@ var ShowUsage = func() { fmt.Println("\nOptions:") flag.PrintDefaults() fmt.Println("\nExamples:") - fmt.Printf("%s 8.8.8.8\t\t\tReturn the country ISO code and name\n", os.Args[0]) - fmt.Printf("%s -d ~/GeoIP 8.8.8.8\t\tUse a different database directory\n", os.Args[0]) - fmt.Printf("%s -i 8.8.8.8\t\t\tReturn just the country ISO code\n", os.Args[0]) - fmt.Printf("%s -c 8.8.8.8\t\t\tReturn just the country name\n", os.Args[0]) - fmt.Printf("%s db-update\t\t\tUpdate the GeoLite2-Country database (do not run more than once a month)\n", os.Args[0]) - fmt.Printf("%s self-update\t\t\tUpdate the GoIpLookup binary with the latest release\n", os.Args[0]) + fmt.Printf("%s 8.8.8.8 # Return the country ISO code and name\n", os.Args[0]) + fmt.Printf("%s -d ~/GeoIP 8.8.8.8 # Use a different database directory\n", os.Args[0]) + fmt.Printf("%s -i 8.8.8.8 # Return just the country ISO code\n", os.Args[0]) + fmt.Printf("%s -c 8.8.8.8 # Return just the country name\n", os.Args[0]) + fmt.Printf("%s db-update # Update the GeoLite2-Country database (do not run more than once a month)\n", os.Args[0]) + fmt.Printf("%s self-update # Update the GoIpLookup binary with the latest release\n", os.Args[0]) } diff --git a/updater.go b/updater.go index d27396a..d45de8b 100644 --- a/updater.go +++ b/updater.go @@ -23,12 +23,12 @@ func UpdateGeoLite2Country() { gzfile := filepath.Join(tmpDir, "GeoLite2-Country.tar.gz") // check the output directory is writeable - if _, err := os.Stat(*dataDir); os.IsNotExist(err) { - os.MkdirAll(*dataDir, os.ModePerm) + if _, err := os.Stat(dataDir); os.IsNotExist(err) { + os.MkdirAll(dataDir, os.ModePerm) } - if _, err := os.Stat(*dataDir); err != nil { - fmt.Println("Error: Cannot create", *dataDir) + if _, err := os.Stat(dataDir); err != nil { + fmt.Println("Error: Cannot create", dataDir) os.Exit(1) } @@ -37,7 +37,7 @@ func UpdateGeoLite2Country() { os.Exit(1) } - if err := ExtractDatabaseFile(*dataDir, gzfile); err != nil { + if err := ExtractDatabaseFile(dataDir, gzfile); err != nil { fmt.Println(err) os.Exit(1) } diff --git a/utils.go b/utils.go index 7c4a174..7949670 100644 --- a/utils.go +++ b/utils.go @@ -52,17 +52,17 @@ func Lookup(lookup string) { os.Exit(1) } - fi, err := os.Stat(*dataDir) + fi, err := os.Stat(dataDir) if err != nil { - fmt.Println("Error: Directory does not exist", *dataDir) + fmt.Println("Error: Directory does not exist", dataDir) os.Exit(1) } switch mode := fi.Mode(); { case mode.IsDir(): // if dataDir is dir, append GeoLite2-Country.mmdb - mmdb = path.Join(*dataDir, "GeoLite2-Country.mmdb") + mmdb = path.Join(dataDir, "GeoLite2-Country.mmdb") case mode.IsRegular(): - mmdb = *dataDir + mmdb = dataDir } Verbose(fmt.Sprintf("Opening %s", mmdb))