feat: Add Cobra cli
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
b97a5e415a
commit
d5eba1904e
8 changed files with 101 additions and 23 deletions
0
LICENSE
Normal file
0
LICENSE
Normal file
57
cmd/now.go
Normal file
57
cmd/now.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
24
cmd/root.go
Normal file
24
cmd/root.go
Normal file
|
@ -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")
|
||||
}
|
21
cmd/yr.go
21
cmd/yr.go
|
@ -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)
|
||||
}
|
3
go.mod
3
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
|
||||
)
|
||||
|
|
8
go.sum
8
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=
|
||||
|
|
7
main.go
Normal file
7
main.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package main
|
||||
|
||||
import "git.sr.ht/~timharek/yr-go/cmd"
|
||||
|
||||
func main() {
|
||||
cmd.Execute()
|
||||
}
|
|
@ -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) {
|
Loading…
Reference in a new issue