[BUGFIX] Transparency is now properly set
* Bugfix brought about by writing the unittest * Headers are now set, to allow user override values (unittest) * Changes to code to allow unittest
This commit is contained in:
parent
8c1fb7f177
commit
bd2dd75169
1 changed files with 15 additions and 14 deletions
29
main.go
29
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
||||||
|
@ -44,7 +45,7 @@ func (w weather) formatHeader(req *http.Request, headers map[string]string) *htt
|
||||||
req.Header.Set("User-Agent", "curl")
|
req.Header.Set("User-Agent", "curl")
|
||||||
|
|
||||||
for key, value := range headers {
|
for key, value := range headers {
|
||||||
req.Header.Add(key, value)
|
req.Header.Set(key, value)
|
||||||
}
|
}
|
||||||
return req
|
return req
|
||||||
}
|
}
|
||||||
|
@ -108,45 +109,45 @@ func getWheather(url string, headers map[string]string, download bool) {
|
||||||
// provided and translate them into uri parameters.
|
// provided and translate them into uri parameters.
|
||||||
// This will figure out if we need to download the PNG image
|
// This will figure out if we need to download the PNG image
|
||||||
// as well.
|
// as well.
|
||||||
func generateParamFormat() (string, bool, error) {
|
func generateParamFormat(switchesF []*bool, freeStyleF *string, formatF *string, oneLinerF *bool, pngF *bool, addFrameF *bool, transparencyF *int, downloadF *bool) (string, bool, error) {
|
||||||
var params strings.Builder
|
var params strings.Builder
|
||||||
var prefix = "?"
|
var prefix = "?"
|
||||||
var downloadMode = *download
|
var downloadMode = *downloadF
|
||||||
|
|
||||||
// If --free-syle is specified, let's not bother and simply return it
|
// If --free-syle is specified, let's not bother and simply return it
|
||||||
if *freeStyle != "" {
|
if *freeStyleF != "" {
|
||||||
return *freeStyle, downloadMode, nil
|
return *freeStyleF, downloadMode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// If --format is specified, let's return the format the user wants
|
// If --format is specified, let's return the format the user wants
|
||||||
if *format != "" {
|
if *formatF != "" {
|
||||||
var str strings.Builder
|
var str strings.Builder
|
||||||
str.WriteString("?format=\"")
|
str.WriteString("?format=\"")
|
||||||
str.WriteString(*format)
|
str.WriteString(*formatF)
|
||||||
str.WriteString("\"")
|
str.WriteString("\"")
|
||||||
return str.String(), downloadMode, nil
|
return str.String(), downloadMode, nil
|
||||||
}
|
}
|
||||||
// If --one-liner is specified, we know the format is set so let's return it
|
// If --one-liner is specified, we know the format is set so let's return it
|
||||||
if *oneLiner {
|
if *oneLinerF {
|
||||||
return "?format=3", downloadMode, nil
|
return "?format=3", downloadMode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, s := range switches {
|
for i, s := range switchesF {
|
||||||
if *s {
|
if *s {
|
||||||
params.WriteString(switchesFlags[i])
|
params.WriteString(switchesFlags[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if *png {
|
if *pngF {
|
||||||
prefix = "_"
|
prefix = "_"
|
||||||
|
|
||||||
if *addFrame {
|
if *addFrameF {
|
||||||
params.WriteString(switchesFlags[12])
|
params.WriteString(switchesFlags[12])
|
||||||
}
|
}
|
||||||
|
|
||||||
if *transparency >= 0 && *transparency <= 255 {
|
if *transparencyF >= 0 && *transparencyF <= 255 {
|
||||||
params.WriteString("_transparency=")
|
params.WriteString("_transparency=")
|
||||||
params.WriteString(string(*transparency))
|
params.WriteString(strconv.Itoa(*transparencyF))
|
||||||
}
|
}
|
||||||
|
|
||||||
params.WriteString(".png")
|
params.WriteString(".png")
|
||||||
|
@ -313,7 +314,7 @@ func generateURL(domain string, v2 bool, location string, lang string, affix str
|
||||||
func main() {
|
func main() {
|
||||||
var domain string = "wttr.in"
|
var domain string = "wttr.in"
|
||||||
flagParser()
|
flagParser()
|
||||||
affix, downloadFile, _ := generateParamFormat()
|
affix, downloadFile, _ := generateParamFormat(switches, freeStyle, format, oneLiner, png, addFrame, transparency, download)
|
||||||
link, headers := generateURL(domain, *v2, *location, *language, affix)
|
link, headers := generateURL(domain, *v2, *location, *language, affix)
|
||||||
getWheather(link, headers, downloadFile)
|
getWheather(link, headers, downloadFile)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue