[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:
Elia el Lazkani 2020-03-01 02:11:58 +01:00
parent 8c1fb7f177
commit bd2dd75169

29
main.go
View file

@ -7,6 +7,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
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")
for key, value := range headers {
req.Header.Add(key, value)
req.Header.Set(key, value)
}
return req
}
@ -108,45 +109,45 @@ func getWheather(url string, headers map[string]string, download bool) {
// provided and translate them into uri parameters.
// This will figure out if we need to download the PNG image
// 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 prefix = "?"
var downloadMode = *download
var downloadMode = *downloadF
// If --free-syle is specified, let's not bother and simply return it
if *freeStyle != "" {
return *freeStyle, downloadMode, nil
if *freeStyleF != "" {
return *freeStyleF, downloadMode, nil
}
// If --format is specified, let's return the format the user wants
if *format != "" {
if *formatF != "" {
var str strings.Builder
str.WriteString("?format=\"")
str.WriteString(*format)
str.WriteString(*formatF)
str.WriteString("\"")
return str.String(), downloadMode, nil
}
// 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
}
for i, s := range switches {
for i, s := range switchesF {
if *s {
params.WriteString(switchesFlags[i])
}
}
if *png {
if *pngF {
prefix = "_"
if *addFrame {
if *addFrameF {
params.WriteString(switchesFlags[12])
}
if *transparency >= 0 && *transparency <= 255 {
if *transparencyF >= 0 && *transparencyF <= 255 {
params.WriteString("_transparency=")
params.WriteString(string(*transparency))
params.WriteString(strconv.Itoa(*transparencyF))
}
params.WriteString(".png")
@ -313,7 +314,7 @@ func generateURL(domain string, v2 bool, location string, lang string, affix str
func main() {
var domain string = "wttr.in"
flagParser()
affix, downloadFile, _ := generateParamFormat()
affix, downloadFile, _ := generateParamFormat(switches, freeStyle, format, oneLiner, png, addFrame, transparency, download)
link, headers := generateURL(domain, *v2, *location, *language, affix)
getWheather(link, headers, downloadFile)
}