Fix gocritic findings

This commit is contained in:
Igor Chubin 2022-12-24 18:10:20 +01:00
parent 9190897277
commit 91e52efa31
4 changed files with 55 additions and 36 deletions

View file

@ -6,7 +6,6 @@ import (
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
@ -60,14 +59,14 @@ type resp struct {
} `json:"data"`
}
func (g *global) getDataFromAPI() resp {
func (g *global) getDataFromAPI() (*resp, error) {
var (
ret resp
params []string
)
if len(g.config.APIKey) == 0 {
log.Fatal("No API key specified. Setup instructions are in the README.")
return nil, fmt.Errorf("No API key specified. Setup instructions are in the README.")
}
params = append(params, "key="+g.config.APIKey)
@ -94,34 +93,41 @@ func (g *global) getDataFromAPI() resp {
res, err := http.Get(wuri + strings.Join(params, "&"))
if err != nil {
log.Fatal(err)
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
return nil, err
}
if g.debug {
var out bytes.Buffer
json.Indent(&out, body, "", " ")
err := json.Indent(&out, body, "", " ")
if err != nil {
return nil, err
}
_, err = out.WriteTo(os.Stderr)
if err != nil {
return nil, err
}
out.WriteTo(os.Stderr)
fmt.Print("\n\n")
}
if g.config.Lang == "" {
if err = json.Unmarshal(body, &ret); err != nil {
log.Println(err)
return nil, err
}
} else {
if err = g.unmarshalLang(body, &ret); err != nil {
log.Println(err)
return nil, err
}
}
return ret
return &ret, nil
}
func (g *global) unmarshalLang(body []byte, r *resp) error {

View file

@ -100,13 +100,16 @@ func (g *global) init() {
g.ansiEsc = regexp.MustCompile("\033.*?m")
}
func Cmd() {
func Cmd() error {
g := global{}
g.init()
flag.Parse()
r := g.getDataFromAPI()
r, err := g.getDataFromAPI()
if err != nil {
return err
}
if r.Data.Req == nil || len(r.Data.Req) < 1 {
if r.Data.Err != nil && len(r.Data.Err) >= 1 {
@ -148,14 +151,20 @@ func Cmd() {
}
if g.config.Numdays == 0 {
return
return nil
}
if r.Data.Weather == nil {
log.Fatal("No detailed weather forecast available.")
}
for _, d := range r.Data.Weather {
for _, val := range g.printDay(d) {
lines, err := g.printDay(d)
if err != nil {
return err
}
for _, val := range lines {
fmt.Fprintln(stdout, val)
}
}
return nil
}

View file

@ -177,10 +177,8 @@ func (g *global) formatWind(c cond) string {
windInRightUnits := func(spd int) int {
if g.config.WindMS {
spd = (spd * 1000) / 3600
} else {
if g.config.Imperial {
spd = (spd * 1000) / 1609
}
} else if g.config.Imperial {
spd = (spd * 1000) / 1609
}
return spd
@ -219,10 +217,8 @@ func (g *global) formatWind(c cond) string {
unitWindString := unitWind(0, g.config.Lang)
if g.config.WindMS {
unitWindString = unitWind(2, g.config.Lang)
} else {
if g.config.Imperial {
unitWindString = unitWind(1, g.config.Lang)
}
} else if g.config.Imperial {
unitWindString = unitWind(1, g.config.Lang)
}
// if (config.Lang == "sl") {
@ -279,12 +275,12 @@ func (g *global) formatCond(cur []string, c cond, current bool) []string {
if g.config.Inverse {
// inverting colors
for i := range icon {
icon[i] = strings.Replace(icon[i], "38;5;226", "38;5;94", -1)
icon[i] = strings.Replace(icon[i], "38;5;250", "38;5;243", -1)
icon[i] = strings.Replace(icon[i], "38;5;21", "38;5;18", -1)
icon[i] = strings.Replace(icon[i], "38;5;255", "38;5;245", -1)
icon[i] = strings.Replace(icon[i], "38;5;111", "38;5;63", -1)
icon[i] = strings.Replace(icon[i], "38;5;251", "38;5;238", -1)
icon[i] = strings.ReplaceAll(icon[i], "38;5;226", "38;5;94")
icon[i] = strings.ReplaceAll(icon[i], "38;5;250", "38;5;243")
icon[i] = strings.ReplaceAll(icon[i], "38;5;21", "38;5;18")
icon[i] = strings.ReplaceAll(icon[i], "38;5;255", "38;5;245")
icon[i] = strings.ReplaceAll(icon[i], "38;5;111", "38;5;63")
icon[i] = strings.ReplaceAll(icon[i], "38;5;251", "38;5;238")
}
}
// desc := fmt.Sprintf("%-15.15v", c.WeatherDesc[0].Value)
@ -328,7 +324,7 @@ func (g *global) formatCond(cur []string, c cond, current bool) []string {
desc = desc[:len(desc)-size] + "…"
// for numberOfSpaces < runewidth.StringWidth(fmt.Sprintf("%c", lastRune)) - 1 {
for runewidth.StringWidth(desc) < 15 {
desc = desc + " "
desc += " "
}
}
}
@ -358,7 +354,7 @@ func justifyCenter(s string, width int) string {
appendSide := 0
for runewidth.StringWidth(s) <= width {
if appendSide == 1 {
s = s + " "
s += " "
appendSide = 0
} else {
s = " " + s

View file

@ -11,7 +11,7 @@ func slotTimes() []int {
return []int{9 * 60, 12 * 60, 18 * 60, 22 * 60}
}
func (g *global) printDay(w weather) []string {
func (g *global) printDay(w weather) ([]string, error) {
var (
ret []string
dateName string
@ -49,7 +49,7 @@ func (g *global) printDay(w weather) []string {
}
ret = g.formatCond(ret, s, false)
for i := range ret {
ret[i] = ret[i] + "│"
ret[i] += "│"
}
}
@ -57,9 +57,15 @@ func (g *global) printDay(w weather) []string {
// dateFmt := "┤ " + d.Format("Mon 02. Jan") + " ├"
if val, ok := locale()[g.config.Lang]; ok {
lctime.SetLocale(val)
err := lctime.SetLocale(val)
if err != nil {
return nil, err
}
} else {
lctime.SetLocale("en_US")
err := lctime.SetLocale("en_US")
if err != nil {
return nil, err
}
}
if g.config.RightToLeft {
@ -107,7 +113,8 @@ func (g *global) printDay(w weather) []string {
ret...)
return append(ret,
"└──────────────────────────────┴──────────────────────────────┘")
"└──────────────────────────────┴──────────────────────────────┘"),
nil
}
if g.config.RightToLeft {
@ -129,5 +136,6 @@ func (g *global) printDay(w weather) []string {
//nolint:lll
return append(ret,
"└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘")
"└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘"),
nil
}