Feature enhancement: Adding -f/--format

* Adding the --format flag to leave it to the users to format their
queries
* Adding an --extra-information flag to print out some advanced
information
* Variable name change to make it more accurate, it's an affix
This commit is contained in:
Elia el Lazkani 2020-02-26 20:07:27 +01:00
parent 202fc60d38
commit d256e127cf
2 changed files with 77 additions and 29 deletions

View file

@ -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.
```

64
main.go
View file

@ -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,7 +232,7 @@ 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
}
@ -250,10 +257,40 @@ var (
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(&params)
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)
}