restructure as submodules
This commit is contained in:
35
main.go
35
main.go
@@ -1,6 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"lookupip/src/ipapi"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
var verbose bool = false
|
var verbose bool = false
|
||||||
var detail bool
|
var detail bool
|
||||||
@@ -13,13 +18,37 @@ func main() {
|
|||||||
if verbose {
|
if verbose {
|
||||||
fmt.Println("Verbose mode on")
|
fmt.Println("Verbose mode on")
|
||||||
}
|
}
|
||||||
data, err := lookup(ip, properties)
|
data, err := ipapi.Lookup(ip, properties)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result := getProperties(data)
|
result := ipapi.GetProperties(data, properties, detail)
|
||||||
|
|
||||||
fmt.Println(result)
|
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, ",")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
package main
|
package ipapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"lookupip/src/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@@ -40,10 +41,10 @@ func buildURL(ip string) string {
|
|||||||
return "http://ip-api.com/json/" + ip
|
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
|
var data *IPAPI
|
||||||
|
|
||||||
if !checkValidIP(ip) {
|
if !utils.CheckValidIP(ip) {
|
||||||
return data, errors.New("please enter a valid IP address")
|
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)
|
resp, err := http.Get(url)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debugOut(Error, err.Error())
|
// DebugOut(Error, err.Error())
|
||||||
|
return data, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&data)
|
err = json.NewDecoder(resp.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
debugOut(Error, err.Error())
|
// DebugOut(Error, err.Error())
|
||||||
|
return data, err
|
||||||
}
|
}
|
||||||
if data.Status == "fail" {
|
if data.Status == "fail" {
|
||||||
debugOut(Error, data.Message)
|
// DebugOut(Error, data.Message)
|
||||||
|
return data, errors.New(data.Message)
|
||||||
}
|
}
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getProperties(data *IPAPI) string {
|
func GetProperties(data *IPAPI, properties []string, detail bool) string {
|
||||||
var result string
|
var result string
|
||||||
var output string = ""
|
var output string = ""
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package ipapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -35,7 +35,7 @@ func TestLookup(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
got, err := lookup(test.ip, []string{"country"})
|
got, err := Lookup(test.ip, []string{"country"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("CheckValidIP(%q) = %v, want %v", test.ip, err, test.want)
|
t.Errorf("CheckValidIP(%q) = %v, want %v", test.ip, err, test.want)
|
||||||
}
|
}
|
||||||
19
src/utils/utils.go
Normal file
19
src/utils/utils.go
Normal 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)
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -16,7 +16,7 @@ func TestCheckValidIP(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
got := checkValidIP(test.ip)
|
got := CheckValidIP(test.ip)
|
||||||
if got != test.want {
|
if got != test.want {
|
||||||
t.Errorf("CheckValidIP(%q) = %v, want %v", test.ip, got, test.want)
|
t.Errorf("CheckValidIP(%q) = %v, want %v", test.ip, got, test.want)
|
||||||
}
|
}
|
||||||
70
utils.go
70
utils.go
@@ -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, ",")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user