Files
lookupip/utils.go
2021-11-10 11:28:54 -06:00

72 lines
1.2 KiB
Go

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) {
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")
}
}
func parseFlags() {
_ip := flag.String("ip", "", "IP address to lookup")
_props := flag.String("p", "country", "Properties to retrieve")
_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, ",")
}
}