yr/cmd/now.go

45 lines
804 B
Go
Raw Normal View History

package cmd
import (
"encoding/json"
"fmt"
"git.sr.ht/~timharek/yr-go/yr"
"github.com/spf13/cobra"
)
var nowCmd = &cobra.Command{
Use: "now <location>",
Aliases: []string{"current", "n"},
Short: "Get current forecasted weather",
Args: cobra.ExactArgs(1),
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)
}