diff --git a/main.go b/main.go index ea52574..30e2e99 100644 --- a/main.go +++ b/main.go @@ -1,24 +1,24 @@ package main import ( + "flag" + "github.com/ryanehamil/lookupip/src/ipapi" "github.com/ryanehamil/lookupip/src/utils" ) -var detail bool -var ip string -var properties string - func main() { - // Parse command line flags - utils.ParseFlags(&ip, &properties, &detail) + ip := flag.String("ip", "", "IP address to lookup") + properties := flag.String("p", "", "Properties to retrieve") + detail := flag.Bool("d", false, "Show Detail") + flag.Parse() // Use the IP-API to lookup the IP address - data, err := ipapi.Lookup(&ip, &properties) + data, err := ipapi.Lookup(*ip) utils.HandleError(err) // Format the data to a string - result := ipapi.GetProperties(data, properties, detail) + result := ipapi.GetProperties(data, *properties, *detail) // Print result with PrintOut utils.PrintOut(result) diff --git a/main_test.go b/main_test.go index 49f4395..a3acf6e 100644 --- a/main_test.go +++ b/main_test.go @@ -21,11 +21,10 @@ func TestLookup(t *testing.T) { for _, test := range tests { ip := test.input - properties := "" // Use the IP-API to lookup anything - data, _ := ipapi.Lookup(&ip, &properties) + data, _ := ipapi.Lookup(ip) - got := ipapi.GetProperties(data, properties, detail) + got := ipapi.GetProperties(data, "", false) if test.want == "Any IPV4" { if !utils.CheckValidIP(data.Query) { diff --git a/src/ipapi/ip_api.go b/src/ipapi/ip_api.go index 63dd2d9..a8126f0 100644 --- a/src/ipapi/ip_api.go +++ b/src/ipapi/ip_api.go @@ -61,15 +61,9 @@ func buildURL(ip string) (string, error) { // Get IP-API data about IP // // https://ip-api.com/docs/api:json -func Lookup(ip *string, properties *string) (data *IPAPI, err error) { +func Lookup(ip string) (data IPAPI, err error) { - if *ip == "" && *properties == "" { - *properties = "Query" - } else if *properties == "" { - *properties = "Country" - } - - url, error := buildURL(*ip) + url, error := buildURL(ip) if error != nil { return data, error } @@ -93,9 +87,11 @@ func Lookup(ip *string, properties *string) (data *IPAPI, err error) { return data, nil } -func GetProperties(data *IPAPI, properties_string string, detail bool) string { +func GetProperties(data IPAPI, properties_string string, detail bool) (output string) { + if properties_string == "" { + properties_string = "Country" + } result := "" - output := "" properties := strings.Split(properties_string, ",") for _, property := range properties { diff --git a/src/ipapi/ip_api_test.go b/src/ipapi/ip_api_test.go index ddb5bf3..154707d 100644 --- a/src/ipapi/ip_api_test.go +++ b/src/ipapi/ip_api_test.go @@ -47,8 +47,7 @@ func TestLookup(t *testing.T) { } for _, test := range tests { - properties := "" - data, err := Lookup(&test.ip, &properties) + data, err := Lookup(test.ip) got := data.Country if err != nil && err.Error() != test.want { t.Errorf("%q. error-buildURL(%q) = %v, want %v", test.explain, test.ip, got, test.want) @@ -78,7 +77,7 @@ func TestGetProperties(t *testing.T) { // This test relies on the lookup function ip := "8.8.8.8" // Use the IP-API to lookup anything - data, _ := Lookup(&ip, &test.properties) + data, _ := Lookup(ip) got := GetProperties(data, test.properties, false) if got != test.want { diff --git a/src/utils/utils.go b/src/utils/utils.go index 204ff1b..a293370 100644 --- a/src/utils/utils.go +++ b/src/utils/utils.go @@ -1,22 +1,11 @@ package utils import ( - "flag" "fmt" "os" "regexp" ) -// Parse command line flags using go's flag package -// -// https://golang.org/pkg/flag/ -func ParseFlags(ip *string, properties *string, detail *bool) { - flag.StringVar(ip, "ip", "", "IP address to lookup") - flag.StringVar(properties, "p", "", "Properties to retrieve") - flag.BoolVar(detail, "d", false, "Show Detail") - flag.Parse() -} - // For printing to the console // // Currently using fmt.Println