diff --git a/cmd/flags/flags.go b/cmd/flags/flags.go deleted file mode 100644 index 864b668..0000000 --- a/cmd/flags/flags.go +++ /dev/null @@ -1,12 +0,0 @@ -package flags - -const ( - LON = "lon" - LAT = "lat" - JSON = "json" - INTERVAL = "interval" - UTC = "utc" - WEB = "web" - ONE_LINE = "oneline" - DEBUG = "debug" -) diff --git a/cmd/forecast.go b/cmd/forecast.go index 933addb..8ddb6ec 100644 --- a/cmd/forecast.go +++ b/cmd/forecast.go @@ -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( diff --git a/cmd/forecast_helper.go b/cmd/forecast_helper.go index 8c25d45..2fdba4d 100644 --- a/cmd/forecast_helper.go +++ b/cmd/forecast_helper.go @@ -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 diff --git a/cmd/now.go b/cmd/now.go index 57cabe3..c7b8b54 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -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() } diff --git a/cmd/root.go b/cmd/root.go index c1704f1..749dc1c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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) } diff --git a/cmd/today.go b/cmd/today.go index 1b5b2b8..cb2a153 100644 --- a/cmd/today.go +++ b/cmd/today.go @@ -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( diff --git a/cmd/tomorrow.go b/cmd/tomorrow.go index 4db4289..0bad2fe 100644 --- a/cmd/tomorrow.go +++ b/cmd/tomorrow.go @@ -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)