refactor: Move output json to helper func

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-04 20:51:20 +02:00
parent e06a647fd4
commit 13d9c41af3
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
4 changed files with 13 additions and 18 deletions

View file

@ -1,7 +1,6 @@
package cmd
import (
"encoding/json"
"fmt"
"git.sr.ht/~timharek/yr/cmd/flags"
@ -37,10 +36,7 @@ func forecast(cmd *cobra.Command, args []string) {
}
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
outputJson(f.Forecast)
}
t := table.New()

View file

@ -1,6 +1,7 @@
package cmd
import (
"encoding/json"
"fmt"
"os"
@ -57,8 +58,14 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
func openBrowser(u string, f *yr.ForecastResult) {
url := fmt.Sprintf(u, f.Coordinates.Latitude, f.Coordinates.Longitude)
err := browser.OpenURL(url)
if err != nil {
cobra.CheckErr(err)
}
cobra.CheckErr(err)
os.Exit(0)
}
// Returns forecast in pretty printed JSON and exits
func outputJson(v any) {
j, err := json.MarshalIndent(v, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s\n", j)
os.Exit(0)
}

View file

@ -1,7 +1,6 @@
package cmd
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
@ -30,10 +29,7 @@ func now(cmd *cobra.Command, args []string) {
n := f.Forecast[0]
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
outputJson(n)
}
itemTime := n.Time.Local()

View file

@ -1,7 +1,6 @@
package cmd
import (
"encoding/json"
"fmt"
"time"
@ -29,10 +28,7 @@ func today(cmd *cobra.Command, args []string) {
}
if helper.isJson {
j, err := json.MarshalIndent(f, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
outputJson(f.Forecast)
}
t := table.New()