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:
parent
202fc60d38
commit
d256e127cf
2 changed files with 77 additions and 29 deletions
|
@ -36,8 +36,10 @@ Flags:
|
||||||
-P, --png Download a weather PNG image
|
-P, --png Download a weather PNG image
|
||||||
--v2 Use the v2 endpoint
|
--v2 Use the v2 endpoint
|
||||||
-t, --transparency=0 Set transparency level (0-100) (PNG only)
|
-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, --location=LOCATION Specify explicite location
|
||||||
-l, --language=LANGUAGE Specify explicite language
|
-l, --language=LANGUAGE Specify explicite language
|
||||||
|
--extra-information Print extra information
|
||||||
--version Show application version.
|
--version Show application version.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
64
main.go
64
main.go
|
@ -36,6 +36,7 @@ const (
|
||||||
NOCOLORS
|
NOCOLORS
|
||||||
ADDFRAME
|
ADDFRAME
|
||||||
MIDTRANSPARENCY
|
MIDTRANSPARENCY
|
||||||
|
FORMAT
|
||||||
LOCATION
|
LOCATION
|
||||||
LANGUAGE
|
LANGUAGE
|
||||||
PNG
|
PNG
|
||||||
|
@ -144,6 +145,7 @@ func getWheather(url string, headers map[string]string, download bool) {
|
||||||
type cmdParams struct {
|
type cmdParams struct {
|
||||||
Flags int
|
Flags int
|
||||||
Transparency int
|
Transparency int
|
||||||
|
Format string
|
||||||
Location string
|
Location string
|
||||||
Language string
|
Language string
|
||||||
}
|
}
|
||||||
|
@ -154,9 +156,14 @@ type cmdParams struct {
|
||||||
// as well.
|
// as well.
|
||||||
func (cmdP *cmdParams) generateParamFormat() (string, bool, error) {
|
func (cmdP *cmdParams) generateParamFormat() (string, bool, error) {
|
||||||
var params []string
|
var params []string
|
||||||
var prefix []string
|
var affix []string
|
||||||
var download bool
|
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 {
|
if cmdP.Flags&METRIC == METRIC {
|
||||||
params = append(params, MetricSwitch)
|
params = append(params, MetricSwitch)
|
||||||
|
@ -211,7 +218,7 @@ func (cmdP *cmdParams) generateParamFormat() (string, bool, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmdP.Flags&PNG == PNG {
|
if cmdP.Flags&PNG == PNG {
|
||||||
prefix[0] = "_"
|
affix[0] = "_"
|
||||||
|
|
||||||
if cmdP.Flags&MIDTRANSPARENCY == MIDTRANSPARENCY {
|
if cmdP.Flags&MIDTRANSPARENCY == MIDTRANSPARENCY {
|
||||||
params = append(params, MidTransparencySwitch)
|
params = append(params, MidTransparencySwitch)
|
||||||
|
@ -225,7 +232,7 @@ func (cmdP *cmdParams) generateParamFormat() (string, bool, error) {
|
||||||
download = true
|
download = true
|
||||||
}
|
}
|
||||||
|
|
||||||
params = append(prefix, strings.Join(params, ""))
|
params = append(affix, strings.Join(params, ""))
|
||||||
|
|
||||||
return strings.Join(params, ""), download, nil
|
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()
|
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()
|
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()
|
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()
|
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()
|
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
|
// Create a function to parse all the command line parameters
|
||||||
// provided and save them in the parameter struct.
|
// provided and save them in the parameter struct.
|
||||||
func flagParser(cmdP *cmdParams) {
|
func flagParser(cmdP *cmdParams) {
|
||||||
|
@ -263,6 +300,10 @@ func flagParser(cmdP *cmdParams) {
|
||||||
|
|
||||||
kingpin.MustParse(app.Parse(os.Args[1:]))
|
kingpin.MustParse(app.Parse(os.Args[1:]))
|
||||||
|
|
||||||
|
if *extraInformation {
|
||||||
|
printExtraInformation()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
// Windows does not have color encoding
|
// Windows does not have color encoding
|
||||||
// so let's make sure windows users are happy
|
// so let's make sure windows users are happy
|
||||||
term := os.Getenv("TERM")
|
term := os.Getenv("TERM")
|
||||||
|
@ -338,6 +379,11 @@ func flagParser(cmdP *cmdParams) {
|
||||||
cmdP.Flags += VTWO
|
cmdP.Flags += VTWO
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *format != "" {
|
||||||
|
cmdP.Flags += FORMAT
|
||||||
|
cmdP.Format = *format
|
||||||
|
}
|
||||||
|
|
||||||
if *location != "" {
|
if *location != "" {
|
||||||
cmdP.Flags += LOCATION
|
cmdP.Flags += LOCATION
|
||||||
cmdP.Location = *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.
|
// 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 link []string
|
||||||
var headers = make(map[string]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)
|
link = append(link, location)
|
||||||
}
|
}
|
||||||
|
|
||||||
if format != "" {
|
if affix != "" {
|
||||||
link = append(link, format)
|
link = append(link, affix)
|
||||||
}
|
}
|
||||||
|
|
||||||
if lang != "" {
|
if lang != "" {
|
||||||
|
@ -383,7 +429,7 @@ func main() {
|
||||||
var params cmdParams = cmdParams{}
|
var params cmdParams = cmdParams{}
|
||||||
var domain string = "wttr.in"
|
var domain string = "wttr.in"
|
||||||
flagParser(¶ms)
|
flagParser(¶ms)
|
||||||
format, download, _ := params.generateParamFormat()
|
affix, download, _ := params.generateParamFormat()
|
||||||
link, headers := generateURL(domain, params.Flags&VTWO == VTWO, params.Location, params.Language, format)
|
link, headers := generateURL(domain, params.Flags&VTWO == VTWO, params.Location, params.Language, affix)
|
||||||
getWheather(strings.Join(link, ""), headers, download)
|
getWheather(strings.Join(link, ""), headers, download)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue