remove pointers for immutability

This commit is contained in:
2022-01-12 12:30:11 -06:00
parent 9fb0df401e
commit a54d4d6d64
5 changed files with 18 additions and 35 deletions

16
main.go
View File

@@ -1,24 +1,24 @@
package main package main
import ( import (
"flag"
"github.com/ryanehamil/lookupip/src/ipapi" "github.com/ryanehamil/lookupip/src/ipapi"
"github.com/ryanehamil/lookupip/src/utils" "github.com/ryanehamil/lookupip/src/utils"
) )
var detail bool
var ip string
var properties string
func main() { func main() {
// Parse command line flags ip := flag.String("ip", "", "IP address to lookup")
utils.ParseFlags(&ip, &properties, &detail) 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 // Use the IP-API to lookup the IP address
data, err := ipapi.Lookup(&ip, &properties) data, err := ipapi.Lookup(*ip)
utils.HandleError(err) utils.HandleError(err)
// Format the data to a string // Format the data to a string
result := ipapi.GetProperties(data, properties, detail) result := ipapi.GetProperties(data, *properties, *detail)
// Print result with PrintOut // Print result with PrintOut
utils.PrintOut(result) utils.PrintOut(result)

View File

@@ -21,11 +21,10 @@ func TestLookup(t *testing.T) {
for _, test := range tests { for _, test := range tests {
ip := test.input ip := test.input
properties := ""
// Use the IP-API to lookup anything // 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 test.want == "Any IPV4" {
if !utils.CheckValidIP(data.Query) { if !utils.CheckValidIP(data.Query) {

View File

@@ -61,15 +61,9 @@ func buildURL(ip string) (string, error) {
// Get IP-API data about IP // Get IP-API data about IP
// //
// https://ip-api.com/docs/api:json // 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 == "" { url, error := buildURL(ip)
*properties = "Query"
} else if *properties == "" {
*properties = "Country"
}
url, error := buildURL(*ip)
if error != nil { if error != nil {
return data, error return data, error
} }
@@ -93,9 +87,11 @@ func Lookup(ip *string, properties *string) (data *IPAPI, err error) {
return data, nil 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 := "" result := ""
output := ""
properties := strings.Split(properties_string, ",") properties := strings.Split(properties_string, ",")
for _, property := range properties { for _, property := range properties {

View File

@@ -47,8 +47,7 @@ func TestLookup(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
properties := "" data, err := Lookup(test.ip)
data, err := Lookup(&test.ip, &properties)
got := data.Country got := data.Country
if err != nil && err.Error() != test.want { if err != nil && err.Error() != test.want {
t.Errorf("%q. error-buildURL(%q) = %v, want %v", test.explain, test.ip, got, 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 // This test relies on the lookup function
ip := "8.8.8.8" ip := "8.8.8.8"
// Use the IP-API to lookup anything // Use the IP-API to lookup anything
data, _ := Lookup(&ip, &test.properties) data, _ := Lookup(ip)
got := GetProperties(data, test.properties, false) got := GetProperties(data, test.properties, false)
if got != test.want { if got != test.want {

View File

@@ -1,22 +1,11 @@
package utils package utils
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"regexp" "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 // For printing to the console
// //
// Currently using fmt.Println // Currently using fmt.Println