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 (
"fmt"
"git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"github.com/spf13/cobra"
)
@ -16,18 +15,19 @@ var forecastCmd = &cobra.Command{
Run: forecast,
}
var (
interval int
)
func init() {
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) {
helper := forecastHelper(cmd, args)
f := helper.f
f := forecastHelper(args)
interval, _ := cmd.Flags().GetInt(flags.INTERVAL)
if helper.isWeb {
if isWeb {
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]
}
if helper.isJson {
if isJson {
outputJson(f.Forecast)
}
t := table.New()
for _, item := range f.Forecast {
if !helper.isUTC {
if !isUTC {
item.Time = item.Time.Local()
}
t.Row(

View file

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

View file

@ -4,7 +4,6 @@ import (
"fmt"
"os"
"git.sr.ht/~timharek/yr/cmd/flags"
"github.com/spf13/cobra"
)
@ -16,29 +15,29 @@ var nowCmd = &cobra.Command{
Run: now,
}
var (
isOneLine bool
)
func init() {
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) {
helper := forecastHelper(cmd, args)
f := helper.f
f := forecastHelper(args)
isOneLine, err := cmd.Flags().GetBool(flags.ONE_LINE)
cobra.CheckErr(err)
if helper.isWeb {
if isWeb {
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
}
n := f.Forecast[0]
if helper.isJson {
if isJson {
outputJson(n)
}
if !helper.isUTC {
if !isUTC {
n.Time = n.Time.Local()
}

View file

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

View file

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

View file

@ -25,10 +25,9 @@ func init() {
}
func tomorrow(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args)
f := helper.f
f := forecastHelper(args)
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)
err := browser.OpenURL(url)
if err != nil {
@ -46,7 +45,7 @@ func tomorrow(cmd *cobra.Command, args []string) {
if item.Time.Format(time.DateOnly) != tomorrow.Format(time.DateOnly) {
continue
}
if !helper.isUTC {
if !isUTC {
item.Time = item.Time.Local()
}
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, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)