[2] | 1 | // $TheSupernovaDuo: yuuka,v 0.1.1 2023/5/29 18:19:14 yakumo_izuru Exp $
|
---|
[1] | 2 | package main
|
---|
| 3 |
|
---|
| 4 | import (
|
---|
| 5 | "fmt"
|
---|
| 6 | "io"
|
---|
| 7 | "log"
|
---|
| 8 | "net/http"
|
---|
[2] | 9 |
|
---|
| 10 | "github.com/integrii/flaggy"
|
---|
[1] | 11 | )
|
---|
| 12 | var (
|
---|
[2] | 13 | format string = "AT"
|
---|
| 14 | region string
|
---|
[1] | 15 | )
|
---|
| 16 | func main() {
|
---|
[2] | 17 | flaggy.SetDescription("Yuuka is a wttr.in client")
|
---|
| 18 | flaggy.SetVersion(FullVersion())
|
---|
| 19 |
|
---|
| 20 | forecastCmd := flaggy.NewSubcommand("forecast")
|
---|
| 21 | forecastCmd.String(&format, "f", "format", "View options")
|
---|
| 22 | forecastCmd.String(®ion, "r", "region", "Where to look at")
|
---|
| 23 | forecastCmd.Description = "Check the forecast for a specified location"
|
---|
[1] | 24 |
|
---|
[2] | 25 | flaggy.AttachSubcommand(forecastCmd, 1)
|
---|
[1] | 26 |
|
---|
[2] | 27 | moonCmd := flaggy.NewSubcommand("moon")
|
---|
| 28 | moonCmd.String(&format, "f", "format", "View options")
|
---|
| 29 | moonCmd.Description = "Check the Moon's phases"
|
---|
| 30 |
|
---|
| 31 | flaggy.AttachSubcommand(moonCmd, 1)
|
---|
| 32 |
|
---|
| 33 | flaggy.ShowHelpOnUnexpectedDisable()
|
---|
| 34 | flaggy.Parse()
|
---|
| 35 |
|
---|
| 36 | if forecastCmd.Used {
|
---|
| 37 | ShowForecast(region, format)
|
---|
| 38 | } else if moonCmd.Used {
|
---|
| 39 | ShowMoonPhases(format)
|
---|
| 40 | } else {
|
---|
| 41 | flaggy.ShowHelpAndExit("A subcommand is required")
|
---|
[1] | 42 | }
|
---|
| 43 | }
|
---|
[2] | 44 | func ShowForecast(region string, format string) {
|
---|
| 45 | query := "https://wttr.in/" + region + "?" + format
|
---|
[1] | 46 | resp, err := http.Get(query)
|
---|
| 47 | sanityCheck(err)
|
---|
| 48 | defer resp.Body.Close()
|
---|
| 49 | body, err := io.ReadAll(resp.Body)
|
---|
| 50 | sanityCheck(err)
|
---|
| 51 | fmt.Printf("%s", body)
|
---|
| 52 | }
|
---|
[2] | 53 | func ShowMoonPhases(format string) {
|
---|
| 54 | query := "https://wttr.in/" + "moon" + "?" + format
|
---|
[1] | 55 | resp, err := http.Get(query)
|
---|
| 56 | sanityCheck(err)
|
---|
| 57 | defer resp.Body.Close()
|
---|
| 58 | body, err := io.ReadAll(resp.Body)
|
---|
| 59 | sanityCheck(err)
|
---|
| 60 | fmt.Printf("%s\n", body)
|
---|
| 61 | }
|
---|
| 62 | func sanityCheck(err error) {
|
---|
| 63 | if err != nil {
|
---|
| 64 | log.Fatal(err)
|
---|
| 65 | }
|
---|
| 66 | }
|
---|