remove pointers for immutability
This commit is contained in:
16
main.go
16
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)
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user