diff --git a/cmd/forecast.go b/cmd/forecast.go index a418a90..35d33b1 100644 --- a/cmd/forecast.go +++ b/cmd/forecast.go @@ -3,11 +3,9 @@ package cmd import ( "encoding/json" "fmt" - "os" "git.sr.ht/~timharek/yr/cmd/flags" "git.sr.ht/~timharek/yr/cmd/internal/ui/table" - "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -31,12 +29,7 @@ func forecast(cmd *cobra.Command, args []string) { interval, _ := cmd.Flags().GetInt(flags.INTERVAL) 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 { - cobra.CheckErr(err) - } - os.Exit(0) + openBrowser("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f) } if interval > 0 { diff --git a/cmd/forecast_helper.go b/cmd/forecast_helper.go index 3145358..cbaf343 100644 --- a/cmd/forecast_helper.go +++ b/cmd/forecast_helper.go @@ -7,6 +7,7 @@ import ( "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" ) @@ -51,3 +52,13 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH { f, } } + +// Opens u in browser with added coordinates and exits +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) + } + os.Exit(0) +} diff --git a/cmd/now.go b/cmd/now.go index 3b9815d..2c0d455 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -3,9 +3,7 @@ package cmd import ( "encoding/json" "fmt" - "os" - "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -26,12 +24,7 @@ func now(cmd *cobra.Command, args []string) { f := helper.f 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 { - cobra.CheckErr(err) - } - os.Exit(0) + openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f) } n := f.Forecast[0] diff --git a/cmd/today.go b/cmd/today.go index ddb3f53..7369d0c 100644 --- a/cmd/today.go +++ b/cmd/today.go @@ -3,11 +3,9 @@ package cmd import ( "encoding/json" "fmt" - "os" "time" "git.sr.ht/~timharek/yr/cmd/internal/ui/table" - "github.com/pkg/browser" "github.com/spf13/cobra" ) @@ -27,12 +25,7 @@ func today(cmd *cobra.Command, args []string) { f := helper.f 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 { - cobra.CheckErr(err) - } - os.Exit(0) + openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f) } if helper.isJson {