yr/cmd/now.go

51 lines
938 B
Go
Raw Normal View History

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")
cobra.CheckErr(err)
location := args[0]
yr, err := yr.New()
cobra.CheckErr(err)
now, err := yr.Now(location)
cobra.CheckErr(err)
if isJson {
j, err := json.MarshalIndent(now, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
}
fmt.Printf(`Current forecast for %s
Temperature: %.1f
Rain: %.1f`, now.Location, now.Temperature, now.Percipitation)
}