diff --git a/cmd/forecast.go b/cmd/forecast.go index 27d79ad..a418a90 100644 --- a/cmd/forecast.go +++ b/cmd/forecast.go @@ -7,8 +7,6 @@ import ( "git.sr.ht/~timharek/yr/cmd/flags" "git.sr.ht/~timharek/yr/cmd/internal/ui/table" - "git.sr.ht/~timharek/yr/internal/nominatim" - "git.sr.ht/~timharek/yr/yr" "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -27,34 +25,12 @@ func init() { } func forecast(cmd *cobra.Command, args []string) { - isJson, err := cmd.Flags().GetBool(flags.JSON) - 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) + helper := forecastHelper(cmd, args) + f := helper.f + interval, _ := cmd.Flags().GetInt(flags.INTERVAL) - c, err := yr.New() - cobra.CheckErr(err) - - if len(args) == 0 && (lon == 0 || lat == 0) { - fmt.Fprintln(os.Stderr, "No location or coordinates provided.") - os.Exit(1) - } - f := &yr.ForecastResult{} - if len(args) == 0 { - f, err = c.ForecastCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) - cobra.CheckErr(err) - } else { - location := args[0] - f, err = c.Forecast(location) - cobra.CheckErr(err) - } - - if isWeb { + if helper.isWeb { url := fmt.Sprintf("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f.Coordinates.Latitude, f.Coordinates.Longitude) err := browser.OpenURL(url) if err != nil { @@ -67,7 +43,7 @@ func forecast(cmd *cobra.Command, args []string) { f.Forecast = f.Forecast[:interval] } - if isJson { + if helper.isJson { j, err := json.MarshalIndent(f, "", " ") cobra.CheckErr(err) fmt.Printf("%s", j) @@ -78,7 +54,7 @@ func forecast(cmd *cobra.Command, args []string) { for _, item := range f.Forecast { itemTime := item.Time.Local() - if isUTC { + if helper.isUTC { itemTime = item.Time } t.Row( diff --git a/cmd/forecast_helper.go b/cmd/forecast_helper.go new file mode 100644 index 0000000..3145358 --- /dev/null +++ b/cmd/forecast_helper.go @@ -0,0 +1,53 @@ +package cmd + +import ( + "fmt" + "os" + + "git.sr.ht/~timharek/yr/cmd/flags" + "git.sr.ht/~timharek/yr/internal/nominatim" + "git.sr.ht/~timharek/yr/yr" + "github.com/spf13/cobra" +) + +type forecastH struct { + isJson 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) + 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() + cobra.CheckErr(err) + + if len(args) == 0 && (lon == 0 || lat == 0) { + fmt.Fprintln(os.Stderr, "No location or coordinates provided.") + os.Exit(1) + } + f := &yr.ForecastResult{} + if len(args) == 0 { + f, err = c.ForecastCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) + cobra.CheckErr(err) + } else { + location := args[0] + f, err = c.Forecast(location) + cobra.CheckErr(err) + } + + return &forecastH{ + isJson, + isUTC, + isWeb, + f, + } +} diff --git a/cmd/now.go b/cmd/now.go index bbe0caf..3b9815d 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -5,9 +5,6 @@ import ( "fmt" "os" - "git.sr.ht/~timharek/yr/cmd/flags" - "git.sr.ht/~timharek/yr/internal/nominatim" - "git.sr.ht/~timharek/yr/yr" "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -25,35 +22,10 @@ func init() { } func now(cmd *cobra.Command, args []string) { - isJson, err := cmd.Flags().GetBool(flags.JSON) - 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) + helper := forecastHelper(cmd, args) + f := helper.f - c, err := yr.New() - cobra.CheckErr(err) - - if len(args) == 0 && (lon == 0 || lat == 0) { - fmt.Fprintln(os.Stderr, "No location or coordinates provided.") - os.Exit(1) - } - f := &yr.ForecastResult{} - if len(args) == 0 { - f, err = c.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) - cobra.CheckErr(err) - - } else { - location := args[0] - f, err = c.Now(location) - cobra.CheckErr(err) - - } - - if isWeb { + if helper.isWeb { url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f/?i=0", f.Coordinates.Latitude, f.Coordinates.Longitude) err := browser.OpenURL(url) if err != nil { @@ -64,7 +36,7 @@ func now(cmd *cobra.Command, args []string) { n := f.Forecast[0] - if isJson { + if helper.isJson { j, err := json.MarshalIndent(f, "", " ") cobra.CheckErr(err) fmt.Printf("%s", j) @@ -72,7 +44,7 @@ func now(cmd *cobra.Command, args []string) { } itemTime := n.Time.Local() - if isUTC { + if helper.isUTC { itemTime = n.Time } fmt.Printf( diff --git a/cmd/today.go b/cmd/today.go index 48c3440..ddb3f53 100644 --- a/cmd/today.go +++ b/cmd/today.go @@ -6,10 +6,7 @@ import ( "os" "time" - "git.sr.ht/~timharek/yr/cmd/flags" "git.sr.ht/~timharek/yr/cmd/internal/ui/table" - "git.sr.ht/~timharek/yr/internal/nominatim" - "git.sr.ht/~timharek/yr/yr" "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -26,33 +23,10 @@ func init() { } func today(cmd *cobra.Command, args []string) { - isJson, err := cmd.Flags().GetBool(flags.JSON) - 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) + helper := forecastHelper(cmd, args) + f := helper.f - c, err := yr.New() - cobra.CheckErr(err) - - if len(args) == 0 && (lon == 0 || lat == 0) { - fmt.Fprintln(os.Stderr, "No location or coordinates provided.") - os.Exit(1) - } - f := &yr.ForecastResult{} - if len(args) == 0 { - f, err = c.ForecastCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil) - cobra.CheckErr(err) - } else { - location := args[0] - f, err = c.Forecast(location) - cobra.CheckErr(err) - } - - if isWeb { + if helper.isWeb { url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f.Coordinates.Latitude, f.Coordinates.Longitude) err := browser.OpenURL(url) if err != nil { @@ -61,7 +35,7 @@ func today(cmd *cobra.Command, args []string) { os.Exit(0) } - if isJson { + if helper.isJson { j, err := json.MarshalIndent(f, "", " ") cobra.CheckErr(err) fmt.Printf("%s", j) @@ -76,7 +50,7 @@ func today(cmd *cobra.Command, args []string) { continue } itemTime := item.Time.Local() - if isUTC { + if helper.isUTC { itemTime = item.Time } t.Row(