From 687d3e8be7020f10c48dcc7057fd5b7e55c47527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Fri, 4 Oct 2024 23:26:35 +0200 Subject: [PATCH] feat: Open yr.no on `yr --web` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements: https://todo.sr.ht/~timharek/yr/3 Signed-off-by: Tim HĂ„rek Andreassen --- cmd/forecast_helper.go | 5 ++++- cmd/root.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/forecast_helper.go b/cmd/forecast_helper.go index bb4d105..ab7c592 100644 --- a/cmd/forecast_helper.go +++ b/cmd/forecast_helper.go @@ -66,7 +66,10 @@ func forecastHelper(cmd *cobra.Command, args []string) *forecastH { // 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) + url := u + if f != nil { + url = fmt.Sprintf(u, f.Coordinates.Latitude, f.Coordinates.Longitude) + } err := browser.OpenURL(url) cobra.CheckErr(err) os.Exit(0) diff --git a/cmd/root.go b/cmd/root.go index 73a41ac..c1704f1 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,6 +11,7 @@ var rootCmd = &cobra.Command{ Use: "yr", Short: "Get the weather for your desired location.", Long: `Get the weather for your desired location.`, + Run: root, } func Execute() { @@ -28,3 +29,15 @@ func init() { rootCmd.PersistentFlags().Float64P(flags.LON, "x", 0, "Longitude coordinate") rootCmd.PersistentFlags().Float64P(flags.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() + cobra.CheckErr(err) +}