diff --git a/README.md b/README.md index 70aa107..cea2c0d 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,10 @@ Flags: -P, --png Download a weather PNG image --v2 Use the v2 endpoint -t, --transparency=0 Set transparency level (0-100) (PNG only) + -f, --format=FORMAT Query format (overrides everything else) -L, --location=LOCATION Specify explicite location -l, --language=LANGUAGE Specify explicite language + --extra-information Print extra information --version Show application version. ``` diff --git a/main.go b/main.go index ae6b1c2..89e71ac 100644 --- a/main.go +++ b/main.go @@ -36,6 +36,7 @@ const ( NOCOLORS ADDFRAME MIDTRANSPARENCY + FORMAT LOCATION LANGUAGE PNG @@ -144,6 +145,7 @@ func getWheather(url string, headers map[string]string, download bool) { type cmdParams struct { Flags int Transparency int + Format string Location string Language string } @@ -154,9 +156,14 @@ type cmdParams struct { // as well. func (cmdP *cmdParams) generateParamFormat() (string, bool, error) { var params []string - var prefix []string + var affix []string var download bool - prefix = append(prefix, "?") + affix = append(affix, "?") + + // If --format is specified, let's not bother and simply return it + if cmdP.Flags&FORMAT == FORMAT { + return cmdP.Format, false, nil + } if cmdP.Flags&METRIC == METRIC { params = append(params, MetricSwitch) @@ -211,7 +218,7 @@ func (cmdP *cmdParams) generateParamFormat() (string, bool, error) { } if cmdP.Flags&PNG == PNG { - prefix[0] = "_" + affix[0] = "_" if cmdP.Flags&MIDTRANSPARENCY == MIDTRANSPARENCY { params = append(params, MidTransparencySwitch) @@ -225,35 +232,65 @@ func (cmdP *cmdParams) generateParamFormat() (string, bool, error) { download = true } - params = append(prefix, strings.Join(params, "")) + params = append(affix, strings.Join(params, "")) return strings.Join(params, ""), download, nil } // Defining the command-line interface var ( - app = kingpin.New("go-cmw", "A small terminal wrapper around the wttr.in weather endpoint.") - metric = app.Flag("metric", "Display weather in metric").Short('m').Default("false").Bool() - uscs = app.Flag("uscs", "Display weather in imperial").Short('u').Default("false").Bool() - meterSecond = app.Flag("meter-second", "Display wind in m/s").Short('M').Default("false").Bool() - zero = app.Flag("zero", "Show the weather now").Short('z').Default("false").Bool() - one = app.Flag("one", "Show the weather for one day").Short('o').Default("false").Bool() - two = app.Flag("two", "Show the weather for two days").Short('w').Default("false").Bool() - ignoreUserAgent = app.Flag("ignore-user-agent", "Request ignoring the user agent").Short('A').Default("false").Bool() - followLink = app.Flag("follow-link", "Follow link redirect").Short('F').Default("true").Bool() - narrow = app.Flag("narrow", "Display weather in narrow view").Short('n').Default("false").Bool() - quiet = app.Flag("quiet", "Add the quiet flag").Short('q').Default("false").Bool() - superQuiet = app.Flag("super-quiet", "Add the super quiet flag").Short('Q').Default("false").Bool() - noColors = app.Flag("no-colors", "Disable displaying colors (always enabled on windows").Short('N').Default("false").Bool() - addFrame = app.Flag("add-frame", "Add a frame to the output").Short('p').Default("false").Bool() - midTransparency = app.Flag("mid-transparency", "Enable mid-transparency (PNG only)").Short('T').Default("false").Bool() - png = app.Flag("png", "Download a weather PNG image").Short('P').Default("false").Bool() - v2 = app.Flag("v2", "Use the v2 endpoint").Default("false").Bool() - transparency = app.Flag("transparency", "Set transparency level (0-100) (PNG only)").Short('t').Default("0").Int() - location = app.Flag("location", "Specify explicite location").Short('L').OverrideDefaultFromEnvar("GO_CMW_LOCATION").String() - language = app.Flag("language", "Specify explicite language").Short('l').OverrideDefaultFromEnvar("GO_CMW_LANGUAGE").String() + app = kingpin.New("go-cmw", "A small terminal wrapper around the wttr.in weather endpoint.") + metric = app.Flag("metric", "Display weather in metric").Short('m').Default("false").Bool() + uscs = app.Flag("uscs", "Display weather in imperial").Short('u').Default("false").Bool() + meterSecond = app.Flag("meter-second", "Display wind in m/s").Short('M').Default("false").Bool() + zero = app.Flag("zero", "Show the weather now").Short('z').Default("false").Bool() + one = app.Flag("one", "Show the weather for one day").Short('o').Default("false").Bool() + two = app.Flag("two", "Show the weather for two days").Short('w').Default("false").Bool() + ignoreUserAgent = app.Flag("ignore-user-agent", "Request ignoring the user agent").Short('A').Default("false").Bool() + followLink = app.Flag("follow-link", "Follow link redirect").Short('F').Default("true").Bool() + narrow = app.Flag("narrow", "Display weather in narrow view").Short('n').Default("false").Bool() + quiet = app.Flag("quiet", "Add the quiet flag").Short('q').Default("false").Bool() + superQuiet = app.Flag("super-quiet", "Add the super quiet flag").Short('Q').Default("false").Bool() + noColors = app.Flag("no-colors", "Disable displaying colors (always enabled on windows").Short('N').Default("false").Bool() + addFrame = app.Flag("add-frame", "Add a frame to the output").Short('p').Default("false").Bool() + midTransparency = app.Flag("mid-transparency", "Enable mid-transparency (PNG only)").Short('T').Default("false").Bool() + png = app.Flag("png", "Download a weather PNG image").Short('P').Default("false").Bool() + v2 = app.Flag("v2", "Use the v2 endpoint").Default("false").Bool() + transparency = app.Flag("transparency", "Set transparency level (0-100) (PNG only)").Short('t').Default("0").Int() + format = app.Flag("format", "Query format (overrides everything else)").Short('f').OverrideDefaultFromEnvar("GO_CMW_FORMAT").String() + location = app.Flag("location", "Specify explicite location").Short('L').OverrideDefaultFromEnvar("GO_CMW_LOCATION").String() + language = app.Flag("language", "Specify explicite language").Short('l').OverrideDefaultFromEnvar("GO_CMW_LANGUAGE").String() + extraInformation = app.Flag("extra-information", "Print extra information").Default("false").Bool() ) +// Special help menu +func printExtraInformation() { + eInfo := ` +Supported Location Types +------------------------ +City name: Paris +Unicode name: Москва +Airport code (3 letters): muc +Domain name: @stackoverflow.com +Area code: 94107 +GPS coordinates: -78.46,106.79 + +Special Location +---------------- +Moon phase (add ,+US or +,+France for these cities): moon +Moon phase for a date: moon@2016-10-25 + +Supported languages +------------------- + +Supported: af da de el et fr fa hu id it nb nl + pl pt-br ro ru tr uk vi + ` + + println(eInfo) +} + // Create a function to parse all the command line parameters // provided and save them in the parameter struct. func flagParser(cmdP *cmdParams) { @@ -263,6 +300,10 @@ func flagParser(cmdP *cmdParams) { kingpin.MustParse(app.Parse(os.Args[1:])) + if *extraInformation { + printExtraInformation() + os.Exit(0) + } // Windows does not have color encoding // so let's make sure windows users are happy term := os.Getenv("TERM") @@ -338,6 +379,11 @@ func flagParser(cmdP *cmdParams) { cmdP.Flags += VTWO } + if *format != "" { + cmdP.Flags += FORMAT + cmdP.Format = *format + } + if *location != "" { cmdP.Flags += LOCATION cmdP.Location = *location @@ -351,7 +397,7 @@ func flagParser(cmdP *cmdParams) { } // Create a function to generate the url that we'll be calling shortly. -func generateURL(domain string, v2 bool, location string, lang string, format string) ([]string, map[string]string) { +func generateURL(domain string, v2 bool, location string, lang string, affix string) ([]string, map[string]string) { var link []string var headers = make(map[string]string) @@ -367,8 +413,8 @@ func generateURL(domain string, v2 bool, location string, lang string, format st link = append(link, location) } - if format != "" { - link = append(link, format) + if affix != "" { + link = append(link, affix) } if lang != "" { @@ -383,7 +429,7 @@ func main() { var params cmdParams = cmdParams{} var domain string = "wttr.in" flagParser(¶ms) - format, download, _ := params.generateParamFormat() - link, headers := generateURL(domain, params.Flags&VTWO == VTWO, params.Location, params.Language, format) + affix, download, _ := params.generateParamFormat() + link, headers := generateURL(domain, params.Flags&VTWO == VTWO, params.Location, params.Language, affix) getWheather(strings.Join(link, ""), headers, download) }