diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..76e6cc5 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +PREFIX ?= /usr/local +GOFLAGS ?= -v -ldflags "-w -X `go list`.Version=${VERSION} -X `go list`.Commit=${COMMIT} -X `go list`.Build=${BUILD}" -tags "static_build" + +VERSION ?= `git describe --abbrev=0 --tags || echo $VERSION` +COMMIT ?= `git rev-parse --short HEAD || echo $COMMIT` +BUILD ?= `git show -s --pretty=format:%cI` + +all: yuuka + +yuuka: + go build ${GOFLAGS} + strip -s yuuka +clean: + rm -f yuuka +install: + install -Dm0755 yuuka ${DESTDIR}${PREFIX}/bin/yuuka +uninstall: + rm -f ${DESTDIR}${PREFIX}/bin/yuuka + diff --git a/go.mod b/go.mod index bbaea4f..106dbbd 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module marisa.chaotic.ninja/yuuka go 1.20 + +require github.com/integrii/flaggy v1.5.2 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ec4c7d6 --- /dev/null +++ b/go.sum @@ -0,0 +1,5 @@ +github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= +github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/integrii/flaggy v1.5.2 h1:bWV20MQEngo4hWhno3i5Z9ISPxLPKj9NOGNwTWb/8IQ= +github.com/integrii/flaggy v1.5.2/go.mod h1:dO13u7SYuhk910nayCJ+s1DeAAGC1THCMj1uSFmwtQ8= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/main.go b/main.go index 5c5fc98..c8f3691 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// $TheSupernovaDuo: yuuka,v master 2023/5/29 17:54:15 yakumo_izuru Exp $ +// $TheSupernovaDuo: yuuka,v 0.1.1 2023/5/29 18:19:14 yakumo_izuru Exp $ package main import ( @@ -6,34 +6,43 @@ import ( "io" "log" "net/http" - "os" + + "github.com/integrii/flaggy" ) var ( - format = "?AT" - url = "https://wttr.in" + format string = "AT" + region string ) func main() { - if len(os.Args) == 1 { - PrintUsage() - } - - cmd := os.Args[1] - - switch cmd { - case "forecast": - ShowForecast() - case "moon": - ShowMoonPhases() - } -} -func PrintUsage() { - fmt.Println("Yuuka is a wttr.in client") - fmt.Printf("\tforecast\tShow the current weather report according to your region\n") - fmt.Printf("\tmoon\tShow the current phase of the Moon\n") -} + flaggy.SetDescription("Yuuka is a wttr.in client") + flaggy.SetVersion(FullVersion()) -func ShowForecast() { - query := url + format + forecastCmd := flaggy.NewSubcommand("forecast") + forecastCmd.String(&format, "f", "format", "View options") + forecastCmd.String(®ion, "r", "region", "Where to look at") + forecastCmd.Description = "Check the forecast for a specified location" + + flaggy.AttachSubcommand(forecastCmd, 1) + + moonCmd := flaggy.NewSubcommand("moon") + moonCmd.String(&format, "f", "format", "View options") + moonCmd.Description = "Check the Moon's phases" + + flaggy.AttachSubcommand(moonCmd, 1) + + flaggy.ShowHelpOnUnexpectedDisable() + flaggy.Parse() + + if forecastCmd.Used { + ShowForecast(region, format) + } else if moonCmd.Used { + ShowMoonPhases(format) + } else { + flaggy.ShowHelpAndExit("A subcommand is required") + } +} +func ShowForecast(region string, format string) { + query := "https://wttr.in/" + region + "?" + format resp, err := http.Get(query) sanityCheck(err) defer resp.Body.Close() @@ -41,8 +50,8 @@ func ShowForecast() { sanityCheck(err) fmt.Printf("%s", body) } -func ShowMoonPhases() { - query := url + "/Moon" + format +func ShowMoonPhases(format string) { + query := "https://wttr.in/" + "moon" + "?" + format resp, err := http.Get(query) sanityCheck(err) defer resp.Body.Close() diff --git a/version.go b/version.go new file mode 100644 index 0000000..d139a43 --- /dev/null +++ b/version.go @@ -0,0 +1,19 @@ +// $TheSupernovaDuo: yuuka,v 0.1.1 2023/5/29 18:19:14 yakumo_izuru Exp $ +package main + +import ( + "fmt" +) + +var ( + // Version release version + Version = "0.0.1" + + // Commit will be overwritten automatically by the build system + Commit = "HEAD" +) + +// FullVersion display the full version and build +func FullVersion() string { + return fmt.Sprintf("%s@%s", Version, Commit) +}