diff --git a/README.md b/README.md index a333f75..3da9896 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,23 @@ ![](https://raddle.me/submission_images/61e31cb98ba6f807847098560a6b29b2fbdf822069186d2cfe5fa67f747b13f2.gif) # mizuchi -The above animated picture says it all. +The above animated picture says it all. +That aside, this was a full port from a shell script that I have never used seriously. ## Build -`go build -o mizuchi` +It has two flavors, and have slightly different output. + +### For [ip-api.com](https://ip-api.com) + +``` +go build ./cmd/mizuchi-ipapi +``` + +### For [ipinfo.io](https://ipinfo.io) + +``` +go build ./cmd/mizuchi-ipinfo +``` ## Usage Only required argument is `-i` diff --git a/cmd/mizuchi-ipapi/main.go b/cmd/mizuchi-ipapi/main.go new file mode 100644 index 0000000..4e47454 --- /dev/null +++ b/cmd/mizuchi-ipapi/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "net/http" +) +type Status struct { + Continent string `json:"continent"` + Country string `json:"country"` + Region string `json:"region"` + City string `json:"city"` + Zip string `json:"zip"` + Latitude string `json:"lat"` + Longitude string `json:"lon"` + Timezone string `json:"timezone"` + Query string `json:"query"` +} +const ( + url = "http://ip-api.com" +) +var ( + ip_addr string +) +func init() { + flag.StringVar(&ip_addr, "i", "1.1.1.1", "IP address to check") +} +func main() { + flag.Parse() + + if len(ip_addr) == 0 { + fmt.Println("No IP address given, trying default") + } + + var status Status + query_url := url + "/" + ip_addr + resp, err := http.Get(query_url) + if err != nil { + fmt.Println(err) + } + defer resp.Body.Close() + _ = json.NewDecoder(resp.Body).Decode(&status) + fmt.Printf("Continent: %v\nCountry: %v\nRegion: %v\nCity: %v\nZip: %v\nLatitude: %v\nLongitude: %v\nTimezone: %v\nQuery: %v\n", status.Continent, status.Country, status.Region, status.City, status.Zip, status.Latitude, status.Longitude, status.Timezone, status.Query) +} diff --git a/main.go b/cmd/mizuchi-ipinfo/main.go similarity index 100% rename from main.go rename to cmd/mizuchi-ipinfo/main.go