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) +}