Skip to content

Commit

Permalink
Merge branch 'release/0.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
axllent committed Oct 28, 2019
2 parents be122fd + ba6e9e9 commit 5a9b2e1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [0.2.1]

- Fix dataDir flag parsing


## [0.2.0]

- Switch to go modules (go >= 1.11 required)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/).

Expand Down
19 changes: 10 additions & 9 deletions goiplookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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")
Expand Down Expand Up @@ -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])
}
10 changes: 5 additions & 5 deletions updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 5a9b2e1

Please sign in to comment.