refactor: Simplify flags

Use *Var version of cobra flags instead of passing around `cmd` everywhere

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-17 16:36:54 +02:00
parent 53981cae1d
commit f41d630a77
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
7 changed files with 44 additions and 80 deletions

View file

@ -1,12 +0,0 @@
package flags
const (
LON = "lon"
LAT = "lat"
JSON = "json"
INTERVAL = "interval"
UTC = "utc"
WEB = "web"
ONE_LINE = "oneline"
DEBUG = "debug"
)

View file

@ -3,7 +3,6 @@ package cmd
import ( import (
"fmt" "fmt"
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table" "git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,18 +15,19 @@ var forecastCmd = &cobra.Command{
Run: forecast, Run: forecast,
} }
var (
interval int
)
func init() { func init() {
rootCmd.AddCommand(forecastCmd) rootCmd.AddCommand(forecastCmd)
rootCmd.PersistentFlags().IntP(flags.INTERVAL, "i", 0, "Number of hours to output") rootCmd.Flags().IntVarP(&interval, "interval", "i", 0, "Number of hours to output")
} }
func forecast(cmd *cobra.Command, args []string) { func forecast(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args) f := forecastHelper(args)
f := helper.f
interval, _ := cmd.Flags().GetInt(flags.INTERVAL) if isWeb {
if helper.isWeb {
openBrowser("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f) openBrowser("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f)
} }
@ -35,14 +35,14 @@ func forecast(cmd *cobra.Command, args []string) {
f.Forecast = f.Forecast[:interval] f.Forecast = f.Forecast[:interval]
} }
if helper.isJson { if isJson {
outputJson(f.Forecast) outputJson(f.Forecast)
} }
t := table.New() t := table.New()
for _, item := range f.Forecast { for _, item := range f.Forecast {
if !helper.isUTC { if !isUTC {
item.Time = item.Time.Local() item.Time = item.Time.Local()
} }
t.Row( t.Row(

View file

@ -5,33 +5,13 @@ import (
"fmt" "fmt"
"os" "os"
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/pkg/nominatim" "git.sr.ht/~timharek/yr/pkg/nominatim"
"git.sr.ht/~timharek/yr/yr" "git.sr.ht/~timharek/yr/yr"
"github.com/pkg/browser" "github.com/pkg/browser"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type forecastH struct { func forecastHelper(args []string) *yr.ForecastResult {
isJson bool
isDebug bool
isUTC bool
isWeb bool
f *yr.ForecastResult
}
func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
isJson, err := cmd.Flags().GetBool(flags.JSON)
cobra.CheckErr(err)
isDebug, err := cmd.Flags().GetBool(flags.DEBUG)
cobra.CheckErr(err)
isUTC, err := cmd.Flags().GetBool(flags.UTC)
cobra.CheckErr(err)
isWeb, err := cmd.Flags().GetBool(flags.WEB)
cobra.CheckErr(err)
lon, _ := cmd.Flags().GetFloat64(flags.LON)
lat, _ := cmd.Flags().GetFloat64(flags.LAT)
c, err := yr.New() c, err := yr.New()
cobra.CheckErr(err) cobra.CheckErr(err)
@ -59,13 +39,7 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
fmt.Printf("Default cache-dir: %v\n", os.TempDir()) fmt.Printf("Default cache-dir: %v\n", os.TempDir())
} }
return &forecastH{ return f
isJson,
isDebug,
isUTC,
isWeb,
f,
}
} }
// Opens u in browser with added coordinates and exits // Opens u in browser with added coordinates and exits

View file

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"git.sr.ht/~timharek/yr/cmd/flags"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -16,29 +15,29 @@ var nowCmd = &cobra.Command{
Run: now, Run: now,
} }
var (
isOneLine bool
)
func init() { func init() {
rootCmd.AddCommand(nowCmd) rootCmd.AddCommand(nowCmd)
rootCmd.PersistentFlags().Bool(flags.ONE_LINE, false, "Output in a single line") rootCmd.Flags().BoolVar(&isOneLine, "oneline", false, "Output in a single line")
} }
func now(cmd *cobra.Command, args []string) { func now(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args) f := forecastHelper(args)
f := helper.f
isOneLine, err := cmd.Flags().GetBool(flags.ONE_LINE) if isWeb {
cobra.CheckErr(err)
if helper.isWeb {
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f) openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
} }
n := f.Forecast[0] n := f.Forecast[0]
if helper.isJson { if isJson {
outputJson(n) outputJson(n)
} }
if !helper.isUTC { if !isUTC {
n.Time = n.Time.Local() n.Time = n.Time.Local()
} }

View file

@ -3,7 +3,6 @@ package cmd
import ( import (
"os" "os"
"git.sr.ht/~timharek/yr/cmd/flags"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -21,23 +20,29 @@ func Execute() {
} }
} }
var (
isJson bool
isDebug bool
isUTC bool
isWeb bool
lon float64
lat float64
)
func init() { func init() {
rootCmd.PersistentFlags().Bool(flags.JSON, false, "Result in JSON") rootCmd.PersistentFlags().BoolVar(&isJson, "json", false, "Result in JSON")
rootCmd.PersistentFlags().Bool(flags.DEBUG, false, "Display debug info") rootCmd.PersistentFlags().BoolVar(&isDebug, "debug", false, "Display debug info")
rootCmd.PersistentFlags().Bool(flags.UTC, false, "Result times in UTC") rootCmd.PersistentFlags().BoolVar(&isUTC, "utc", false, "Result times in UTC")
rootCmd.PersistentFlags().Bool(flags.WEB, false, "Open result in browser") rootCmd.PersistentFlags().BoolVar(&isWeb, "web", false, "Open result in browser")
rootCmd.PersistentFlags().Float64P(flags.LON, "x", 0, "Longitude coordinate") rootCmd.PersistentFlags().Float64VarP(&lon, "lon", "x", 0, "Longitude coordinate")
rootCmd.PersistentFlags().Float64P(flags.LAT, "y", 0, "Latitude coordinate") rootCmd.PersistentFlags().Float64VarP(&lat, "lat", "y", 0, "Latitude coordinate")
} }
func root(cmd *cobra.Command, args []string) { func root(cmd *cobra.Command, args []string) {
isWeb, err := cmd.Flags().GetBool(flags.WEB)
cobra.CheckErr(err)
if isWeb { if isWeb {
openBrowser("https://www.yr.no/en", nil) openBrowser("https://www.yr.no/en", nil)
} }
err = cmd.Help() err := cmd.Help()
cobra.CheckErr(err) cobra.CheckErr(err)
} }

View file

@ -20,14 +20,13 @@ func init() {
} }
func today(cmd *cobra.Command, args []string) { func today(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args) f := forecastHelper(args)
f := helper.f
if helper.isWeb { if isWeb {
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f) openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
} }
if helper.isJson { if isJson {
outputJson(f.Forecast) outputJson(f.Forecast)
} }
@ -38,7 +37,7 @@ func today(cmd *cobra.Command, args []string) {
if item.Time.Format(time.DateOnly) != today.Format(time.DateOnly) { if item.Time.Format(time.DateOnly) != today.Format(time.DateOnly) {
continue continue
} }
if !helper.isUTC { if !isUTC {
item.Time = item.Time.Local() item.Time = item.Time.Local()
} }
t.Row( t.Row(

View file

@ -25,10 +25,9 @@ func init() {
} }
func tomorrow(cmd *cobra.Command, args []string) { func tomorrow(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args) f := forecastHelper(args)
f := helper.f
if helper.isWeb { if isWeb {
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=1", f.Coordinates.Latitude, f.Coordinates.Longitude) url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=1", f.Coordinates.Latitude, f.Coordinates.Longitude)
err := browser.OpenURL(url) err := browser.OpenURL(url)
if err != nil { if err != nil {
@ -46,7 +45,7 @@ func tomorrow(cmd *cobra.Command, args []string) {
if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) { if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) {
continue continue
} }
if !helper.isUTC { if !isUTC {
item.Time = item.Time.Local() item.Time = item.Time.Local()
} }
tomorrows = append(tomorrows, item) tomorrows = append(tomorrows, item)
@ -58,7 +57,7 @@ func tomorrow(cmd *cobra.Command, args []string) {
) )
} }
if helper.isJson { if isJson {
j, err := json.MarshalIndent(tomorrows, "", " ") j, err := json.MarshalIndent(tomorrows, "", " ")
cobra.CheckErr(err) cobra.CheckErr(err)
fmt.Printf("%s", j) fmt.Printf("%s", j)