restructure as submodules

This commit is contained in:
2021-11-16 15:18:19 -06:00
parent b5450aa278
commit 1267a8f4e3
6 changed files with 66 additions and 84 deletions

35
main.go
View File

@@ -1,6 +1,11 @@
package main
import "fmt"
import (
"flag"
"fmt"
"lookupip/src/ipapi"
"strings"
)
var verbose bool = false
var detail bool
@@ -13,13 +18,37 @@ func main() {
if verbose {
fmt.Println("Verbose mode on")
}
data, err := lookup(ip, properties)
data, err := ipapi.Lookup(ip, properties)
if err != nil {
fmt.Println(err)
return
}
result := getProperties(data)
result := ipapi.GetProperties(data, properties, detail)
fmt.Println(result)
}
func parseFlags() {
_ip := flag.String("ip", "", "IP address to lookup")
_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()
if *_verbose {
verbose = true
}
if *_ip == "" {
if len(_loose) == 0 {
} else {
ip = _loose[0]
}
} else {
ip = *_ip
}
if _props != nil {
properties = strings.Split(*_props, ",")
}
}

View File

@@ -1,8 +1,9 @@
package main
package ipapi
import (
"encoding/json"
"errors"
"lookupip/src/utils"
"net/http"
"reflect"
)
@@ -40,10 +41,10 @@ func buildURL(ip string) string {
return "http://ip-api.com/json/" + ip
}
func lookup(ip string, properties []string) (*IPAPI, error) {
func Lookup(ip string, properties []string) (*IPAPI, error) {
var data *IPAPI
if !checkValidIP(ip) {
if !utils.CheckValidIP(ip) {
return data, errors.New("please enter a valid IP address")
}
@@ -51,21 +52,24 @@ func lookup(ip string, properties []string) (*IPAPI, error) {
resp, err := http.Get(url)
if err != nil {
debugOut(Error, err.Error())
// DebugOut(Error, err.Error())
return data, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
debugOut(Error, err.Error())
// DebugOut(Error, err.Error())
return data, err
}
if data.Status == "fail" {
debugOut(Error, data.Message)
// DebugOut(Error, data.Message)
return data, errors.New(data.Message)
}
return data, nil
}
func getProperties(data *IPAPI) string {
func GetProperties(data *IPAPI, properties []string, detail bool) string {
var result string
var output string = ""

View File

@@ -1,4 +1,4 @@
package main
package ipapi
import (
"testing"
@@ -35,7 +35,7 @@ func TestLookup(t *testing.T) {
}
for _, test := range tests {
got, err := lookup(test.ip, []string{"country"})
got, err := Lookup(test.ip, []string{"country"})
if err != nil {
t.Errorf("CheckValidIP(%q) = %v, want %v", test.ip, err, test.want)
}

19
src/utils/utils.go Normal file
View File

@@ -0,0 +1,19 @@
package utils
import (
"regexp"
)
type Verbosity int
const (
Info Verbosity = iota
Warning
Error
Debug
)
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])$`)
return re.MatchString(ip)
}

View File

@@ -1,4 +1,4 @@
package main
package utils
import (
"testing"
@@ -16,7 +16,7 @@ func TestCheckValidIP(t *testing.T) {
}
for _, test := range tests {
got := checkValidIP(test.ip)
got := CheckValidIP(test.ip)
if got != test.want {
t.Errorf("CheckValidIP(%q) = %v, want %v", test.ip, got, test.want)
}

View File

@@ -1,70 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
"regexp"
"strings"
)
type Verbosity int
const (
Info Verbosity = iota
Warning
Error
Debug
)
func debugOut(level Verbosity, text string) {
switch level {
case Info:
if verbose {
fmt.Println("[INFO] " + text)
} else {
fmt.Println(text)
}
case Warning:
if verbose {
fmt.Println("[WARNING] " + text)
}
case Error:
fmt.Println("[ERROR] " + text)
os.Exit(1)
case Debug:
if verbose {
fmt.Println("[DEBUG] " + text)
}
}
}
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])$`)
return re.MatchString(ip)
}
func parseFlags() {
_ip := flag.String("ip", "", "IP address to lookup")
_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()
if *_verbose {
verbose = true
}
if *_ip == "" {
if len(_loose) == 0 {
debugOut(Error, "Please enter an IP address or use -h for help")
} else {
ip = _loose[0]
}
} else {
ip = *_ip
}
if _props != nil {
properties = strings.Split(*_props, ",")
}
}