refactor returns and function case

This commit is contained in:
2021-11-16 14:58:49 -06:00
parent e459f52f83
commit fadbf23a3f
3 changed files with 68 additions and 26 deletions

View File

@@ -2,17 +2,51 @@ package main
import (
"encoding/json"
"errors"
"net/http"
"reflect"
)
// IP-API data struct
// https://ip-api.com/docs/api:json
type IPAPI struct {
Continent string `json:"continent"`
ContinentCode string `json:"continentCode"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
Zip string `json:"zip"`
Lat float32 `json:"lat"`
Lon float32 `json:"lon"`
Timezone string `json:"timezone"`
Offset int `json:"offset"`
Currency string `json:"currency"`
ISP string `json:"isp"`
Org string `json:"org"`
AS string `json:"as"`
ASName string `json:"asname"`
Reverse string `json:"reverse"`
Hosting bool `json:"hosting"`
Query string `json:"query"`
Status string `json:"status"`
Message string `json:"message"`
}
// Build URL to query IP-API
// https://ip-api.com/docs/api:json
func buildURL(ip string) string {
return "http://ip-api.com/json/" + ip
}
func Lookup(ip string, properties []string) map[string]interface{} {
CheckValidIP(ip)
func lookup(ip string, properties []string) (*IPAPI, error) {
var data *IPAPI
if !checkValidIP(ip) {
return data, errors.New("please enter a valid IP address")
}
url := buildURL(ip)
resp, err := http.Get(url)
@@ -21,36 +55,35 @@ func Lookup(ip string, properties []string) map[string]interface{} {
}
defer resp.Body.Close()
var data map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
debugOut(Error, err.Error())
}
if data["status"] == "fail" {
debugOut(Error, data["message"].(string))
if data.Status == "fail" {
debugOut(Error, data.Message)
}
return data
return data, nil
}
func GetProperties(data map[string]interface{}) string {
func getProperties(data *IPAPI) string {
var result string
var output string = ""
for _, property := range properties {
found := false
for key, value := range data {
if key == property {
if output != "" {
output += ","
}
output += value.(string)
found = true
}
datafield := reflect.Indirect(reflect.ValueOf(data)).FieldByName(property).String()
if datafield != "" {
result = datafield
} else {
result = "Not found"
}
if !found {
debugOut(Error, "Property '"+property+"' not found")
if detail {
output += property + ": " + result + "\n"
} else {
if output != "" {
output += ","
}
output += result
}
}
return output
}

12
main.go
View File

@@ -3,13 +3,23 @@ package main
import "fmt"
var verbose bool = false
var detail bool
var ip string
var properties []string
func main() {
parseFlags()
result := GetProperties(Lookup(ip, properties))
if verbose {
fmt.Println("Verbose mode on")
}
data, err := lookup(ip, properties)
if err != nil {
fmt.Println(err)
return
}
result := getProperties(data)
fmt.Println(result)
}

View File

@@ -39,16 +39,15 @@ func debugOut(level Verbosity, text string) {
}
}
func CheckValidIP(ip string) {
func checkValidIP(ip string) bool {
re := regexp.MustCompile(`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`)
if !re.MatchString(ip) {
debugOut(Error, "Please enter a valid IP address")
}
return re.MatchString(ip)
}
func parseFlags() {
_ip := flag.String("ip", "", "IP address to lookup")
_props := flag.String("p", "country", "Properties to retrieve")
_props := flag.String("p", "Country", "Properties to retrieve")
flag.BoolVar(&detail, "d", false, "Show Detail")
_verbose := flag.Bool("v", false, "Verbose output")
flag.Parse()
_loose := flag.Args()