yr/cmd/root.go
Tim Hårek Andreassen f41d630a77
refactor: Simplify flags
Use *Var version of cobra flags instead of passing around `cmd` everywhere

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-17 16:36:54 +02:00

48 lines
1 KiB
Go

package cmd
import (
"os"
"github.com/spf13/cobra"
)
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() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
var (
isJson bool
isDebug bool
isUTC bool
isWeb bool
lon float64
lat float64
)
func init() {
rootCmd.PersistentFlags().BoolVar(&isJson, "json", false, "Result in JSON")
rootCmd.PersistentFlags().BoolVar(&isDebug, "debug", false, "Display debug info")
rootCmd.PersistentFlags().BoolVar(&isUTC, "utc", false, "Result times in UTC")
rootCmd.PersistentFlags().BoolVar(&isWeb, "web", false, "Open result in browser")
rootCmd.PersistentFlags().Float64VarP(&lon, "lon", "x", 0, "Longitude coordinate")
rootCmd.PersistentFlags().Float64VarP(&lat, "lat", "y", 0, "Latitude coordinate")
}
func root(cmd *cobra.Command, args []string) {
if isWeb {
openBrowser("https://www.yr.no/en", nil)
}
err := cmd.Help()
cobra.CheckErr(err)
}