df389705a0
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
69 lines
1.1 KiB
Go
69 lines
1.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var nowCmd = &cobra.Command{
|
|
Use: "now <location>",
|
|
Aliases: []string{"current", "n"},
|
|
Short: "Current forecasted weather",
|
|
Args: cobra.MaximumNArgs(1),
|
|
Run: now,
|
|
}
|
|
|
|
var (
|
|
isOneLine bool
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(nowCmd)
|
|
rootCmd.Flags().BoolVar(&isOneLine, "oneline", false, "Output in a single line")
|
|
}
|
|
|
|
func now(cmd *cobra.Command, args []string) {
|
|
f := forecastHelper(args)
|
|
|
|
if isWeb {
|
|
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
|
|
}
|
|
|
|
n := f.Forecast[0]
|
|
|
|
if isJson {
|
|
outputJson(n)
|
|
}
|
|
|
|
if !isUTC {
|
|
n.Time = n.Time.Local()
|
|
}
|
|
|
|
if isOneLine {
|
|
fmt.Printf("%s %s: %.1f °C, %.1f mm, %.1f m/s from %s\n",
|
|
n.Location,
|
|
n.Time.Format("Monday 2 Jan 15:04"),
|
|
n.Temperature,
|
|
n.Percipitation,
|
|
n.Wind.Speed,
|
|
n.Wind.DirectionToString(),
|
|
)
|
|
os.Exit(0)
|
|
}
|
|
|
|
fmt.Printf(
|
|
`%s %s:
|
|
Temperature: %.1f °C
|
|
Rain: %.1f mm
|
|
Wind: %.1f m/s from %s
|
|
`,
|
|
n.Location,
|
|
n.Time.Format("Monday 2 Jan 15:04"),
|
|
n.Temperature,
|
|
n.Percipitation,
|
|
n.Wind.Speed,
|
|
n.Wind.DirectionToString(),
|
|
)
|
|
}
|