From d5eba1904e9bc52506ca649f6b73512ccfebafef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sun, 29 Sep 2024 19:20:59 +0200 Subject: [PATCH] feat: Add Cobra cli MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim HĂ„rek Andreassen --- LICENSE | 0 cmd/now.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 24 ++++++++++++++++++++ cmd/yr.go | 21 ----------------- go.mod | 3 +++ go.sum | 8 +++++++ main.go | 7 ++++++ yr.go => yr/yr.go | 4 ++-- 8 files changed, 101 insertions(+), 23 deletions(-) create mode 100644 LICENSE create mode 100644 cmd/now.go create mode 100644 cmd/root.go delete mode 100644 cmd/yr.go create mode 100644 main.go rename yr.go => yr/yr.go (92%) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e69de29 diff --git a/cmd/now.go b/cmd/now.go new file mode 100644 index 0000000..ac1bc1e --- /dev/null +++ b/cmd/now.go @@ -0,0 +1,57 @@ +package cmd + +import ( + "encoding/json" + "errors" + "fmt" + + "git.sr.ht/~timharek/yr-go/yr" + "github.com/spf13/cobra" +) + +var nowCmd = &cobra.Command{ + Use: "now", + Aliases: []string{"current", "n"}, + Short: "Get current forecasted weather", + Args: func(cmd *cobra.Command, args []string) error { + if len(args) < 1 || len(args) > 1 { + return errors.New("Need a maximum of one argument") + } + return nil + }, + Run: now, +} + +func init() { + rootCmd.AddCommand(nowCmd) +} + +func now(cmd *cobra.Command, args []string) { + isJson, err := cmd.Flags().GetBool("json") + check(err) + location := args[0] + + yr, err := yr.New() + check(err) + + now, err := yr.Now(location) + check(err) + + if isJson { + j, err := json.MarshalIndent(now, "", " ") + check(err) + fmt.Printf("%s", j) + return + } + + fmt.Printf(`Current forecast + Temperature: %.4f + Rain: %.4f`, now.Temperature, now.Percipitation) + +} + +func check(err error) { + if err != nil { + panic(err) + } +} diff --git a/cmd/root.go b/cmd/root.go new file mode 100644 index 0000000..a3ea5a6 --- /dev/null +++ b/cmd/root.go @@ -0,0 +1,24 @@ +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.`, +} + +func Execute() { + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func init() { + rootCmd.PersistentFlags().Bool("json", false, "Result in JSON") +} diff --git a/cmd/yr.go b/cmd/yr.go deleted file mode 100644 index c8dc428..0000000 --- a/cmd/yr.go +++ /dev/null @@ -1,21 +0,0 @@ -package main - -import ( - "fmt" - - "git.sr.ht/~timharek/yr-go" -) - -func main() { - y, err := yr.New() - if err != nil { - panic(err) - } - now, err := y.Now("bergen") - if err != nil { - panic(err) - } - - fmt.Printf("Temp: %.4f\n", now.Temperature) - fmt.Printf("Rain: %.4f\n", now.Percipitation) -} diff --git a/go.mod b/go.mod index 6a3dcd2..5915f39 100644 --- a/go.mod +++ b/go.mod @@ -6,9 +6,12 @@ require github.com/stretchr/testify v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rogpeppe/go-internal v1.12.0 // indirect + github.com/spf13/cobra v1.8.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a1dc9ac..6a51f40 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,9 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -14,6 +17,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go new file mode 100644 index 0000000..91cc976 --- /dev/null +++ b/main.go @@ -0,0 +1,7 @@ +package main + +import "git.sr.ht/~timharek/yr-go/cmd" + +func main() { + cmd.Execute() +} diff --git a/yr.go b/yr/yr.go similarity index 92% rename from yr.go rename to yr/yr.go index 498436e..aeb93e2 100644 --- a/yr.go +++ b/yr/yr.go @@ -29,8 +29,8 @@ func New() (*Client, error) { } type NowForecast struct { - Temperature float64 - Percipitation float64 + Temperature float64 `json:"temperature"` + Percipitation float64 `json:"percipitation"` } func (c *Client) Now(q string) (*NowForecast, error) {