diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..61eab31 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +weather-cli diff --git a/go.mod b/go.mod index c6e5d16..0b7516d 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,9 @@ module github.com/giesan/weather-cli -go 1.21.0 +go 1.21 + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/spf13/cobra v1.7.0 // indirect + github.com/spf13/pflag v1.0.5 // indirect +) diff --git a/go.sum b/go.sum index e69de29..f3366a9 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= +github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go index 9bbaff5..65f56f6 100644 --- a/main.go +++ b/main.go @@ -1,44 +1,5 @@ package main -import ( - "flag" - "fmt" - "net/http" - "os" -) - func main() { - - key := os.Getenv("WEATHERAPI_KEY") - if key == "" { - fmt.Println("The environment variable WEATHERAPI_KEY is not set!\nSet this variable and try again.\n\nFor more informations read https://www.weatherapi.com/docs.") - os.Exit(1) - } - - var location *string - location = flag.String("l", "", "Location for which the weather is to be shown") - flag.Parse() - - if *location == "" { - fmt.Println("The location is not set!") - os.Exit(0) - } - - res, err := http.Get("http://api.weatherapi.com/v1/current.json?key=" + key + "&q=" + *location) - if err != nil { - panic(err) - } - defer res.Body.Close() - - if res.StatusCode >= 400 && res.StatusCode <= 410 { - printAPIError(res) - os.Exit(0) - } - - if res.StatusCode != 200 { - panic("Weather API not available") - } - - printWeather(res) - + ExecuteRootCmd() } diff --git a/rootCmd.go b/rootCmd.go new file mode 100644 index 0000000..130e5ba --- /dev/null +++ b/rootCmd.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "github.com/spf13/cobra" + "net/http" + "os" +) + +var rootCmd = &cobra.Command{ + Short: "weather", + Long: "weather", + Run: func(cmd *cobra.Command, args []string) { + + key := os.Getenv("WEATHERAPI_KEY") + if key == "" { + fmt.Println("The environment variable WEATHERAPI_KEY is not set!\nSet this variable and try again.\n\nFor more informations read https://www.weatherapi.com/docs.") + os.Exit(1) + } + + location, _ := cmd.Flags().GetString("l") + res, err := http.Get("http://api.weatherapi.com/v1/current.json?key=" + key + "&q=" + location) + if err != nil { + panic(err) + } + defer res.Body.Close() + + if res.StatusCode >= 400 && res.StatusCode <= 410 { + printAPIError(res) + os.Exit(0) + } + + if res.StatusCode != 200 { + panic("Weather API not available") + } + + printWeather(res) + + }, +} + +func ExecuteRootCmd() { + rootCmd.Flags().String("l", "Berlin", "Location for which the weather is to be shown") + // rootCmd.MarkFlagRequired("l") + rootCmd.Execute() +}