d5eba1904e
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
57 lines
942 B
Go
57 lines
942 B
Go
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)
|
|
}
|
|
}
|