refactor: Move browser opening to function

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-10-04 20:44:57 +02:00
parent 7f72ae918b
commit e06a647fd4
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
4 changed files with 14 additions and 24 deletions

View file

@ -3,11 +3,9 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"git.sr.ht/~timharek/yr/cmd/flags" "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/pkg/browser"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -31,12 +29,7 @@ func forecast(cmd *cobra.Command, args []string) {
interval, _ := cmd.Flags().GetInt(flags.INTERVAL) interval, _ := cmd.Flags().GetInt(flags.INTERVAL)
if helper.isWeb { if helper.isWeb {
url := fmt.Sprintf("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f.Coordinates.Latitude, f.Coordinates.Longitude) openBrowser("https://www.yr.no/en/forecast/daily-table/%.4f,%.4f", f)
err := browser.OpenURL(url)
if err != nil {
cobra.CheckErr(err)
}
os.Exit(0)
} }
if interval > 0 { if interval > 0 {

View file

@ -7,6 +7,7 @@ import (
"git.sr.ht/~timharek/yr/cmd/flags" "git.sr.ht/~timharek/yr/cmd/flags"
"git.sr.ht/~timharek/yr/internal/nominatim" "git.sr.ht/~timharek/yr/internal/nominatim"
"git.sr.ht/~timharek/yr/yr" "git.sr.ht/~timharek/yr/yr"
"github.com/pkg/browser"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -51,3 +52,13 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH {
f, 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)
}

View file

@ -3,9 +3,7 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"github.com/pkg/browser"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -26,12 +24,7 @@ func now(cmd *cobra.Command, args []string) {
f := helper.f f := helper.f
if helper.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) openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
err := browser.OpenURL(url)
if err != nil {
cobra.CheckErr(err)
}
os.Exit(0)
} }
n := f.Forecast[0] n := f.Forecast[0]

View file

@ -3,11 +3,9 @@ package cmd
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"os"
"time" "time"
"git.sr.ht/~timharek/yr/cmd/internal/ui/table" "git.sr.ht/~timharek/yr/cmd/internal/ui/table"
"github.com/pkg/browser"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -27,12 +25,7 @@ func today(cmd *cobra.Command, args []string) {
f := helper.f f := helper.f
if helper.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) openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
err := browser.OpenURL(url)
if err != nil {
cobra.CheckErr(err)
}
os.Exit(0)
} }
if helper.isJson { if helper.isJson {