yr/cmd/now.go
Tim Hårek Andreassen 13d9c41af3
refactor: Move output json to helper func
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
2024-10-04 20:51:20 +02:00

52 lines
852 B
Go

package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var nowCmd = &cobra.Command{
Use: "now <location>",
Aliases: []string{"current", "n"},
Short: "Get current forecasted weather",
Args: cobra.MaximumNArgs(1),
Run: now,
}
func init() {
rootCmd.AddCommand(nowCmd)
}
func now(cmd *cobra.Command, args []string) {
helper := forecastHelper(cmd, args)
f := helper.f
if helper.isWeb {
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
}
n := f.Forecast[0]
if helper.isJson {
outputJson(n)
}
itemTime := n.Time.Local()
if helper.isUTC {
itemTime = n.Time
}
fmt.Printf(
`%s %s:
Temperature: %.1f °C
Rain: %.1f mm
Wind: %.1f m/s from %s
`,
n.Location,
itemTime.Format("Monday 2 Jan 15:04"),
n.Temperature,
n.Percipitation,
n.Wind.Speed,
n.Wind.DirectionToString(),
)
}