yr/cmd/now.go
Tim Hårek Andreassen 1e6eaa4b4d
chore: Better "use"
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-09-29 20:31:42 +02:00

44 lines
804 B
Go

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)
}