2024-09-29 17:20:59 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-04 19:00:06 +00:00
|
|
|
"os"
|
2024-09-29 17:20:59 +00:00
|
|
|
|
2024-10-04 19:00:06 +00:00
|
|
|
"git.sr.ht/~timharek/yr/cmd/flags"
|
2024-09-29 17:20:59 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var nowCmd = &cobra.Command{
|
2024-09-29 18:31:42 +00:00
|
|
|
Use: "now <location>",
|
2024-09-29 17:20:59 +00:00
|
|
|
Aliases: []string{"current", "n"},
|
|
|
|
Short: "Get current forecasted weather",
|
2024-09-29 19:46:34 +00:00
|
|
|
Args: cobra.MaximumNArgs(1),
|
2024-09-29 18:31:42 +00:00
|
|
|
Run: now,
|
2024-09-29 17:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(nowCmd)
|
2024-10-04 19:00:06 +00:00
|
|
|
rootCmd.PersistentFlags().Bool(flags.ONE_LINE, false, "Output in a single line")
|
2024-09-29 17:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func now(cmd *cobra.Command, args []string) {
|
2024-10-04 18:39:05 +00:00
|
|
|
helper := forecastHelper(cmd, args)
|
|
|
|
f := helper.f
|
2024-09-29 17:20:59 +00:00
|
|
|
|
2024-10-04 19:00:06 +00:00
|
|
|
isOneLine, err := cmd.Flags().GetBool(flags.ONE_LINE)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
2024-10-04 18:39:05 +00:00
|
|
|
if helper.isWeb {
|
2024-10-04 18:44:57 +00:00
|
|
|
openBrowser("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f?i=0", f)
|
2024-10-03 14:05:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
n := f.Forecast[0]
|
|
|
|
|
2024-10-04 18:39:05 +00:00
|
|
|
if helper.isJson {
|
2024-10-04 18:51:20 +00:00
|
|
|
outputJson(n)
|
2024-09-29 17:20:59 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 19:04:58 +00:00
|
|
|
itemTime := n.Time.Local()
|
2024-10-04 18:39:05 +00:00
|
|
|
if helper.isUTC {
|
2024-10-02 19:04:58 +00:00
|
|
|
itemTime = n.Time
|
|
|
|
}
|
2024-10-04 19:00:06 +00:00
|
|
|
|
|
|
|
if isOneLine {
|
|
|
|
fmt.Printf("%s %s: %.1f °C, %.1f mm, %.1f m/s from %s\n",
|
|
|
|
n.Location,
|
|
|
|
itemTime.Format("Monday 2 Jan 15:04"),
|
|
|
|
n.Temperature,
|
|
|
|
n.Percipitation,
|
|
|
|
n.Wind.Speed,
|
|
|
|
n.Wind.DirectionToString(),
|
|
|
|
)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2024-09-29 19:46:34 +00:00
|
|
|
fmt.Printf(
|
2024-10-04 19:00:06 +00:00
|
|
|
`%s %s: Temperature: %.1f °C Rain: %.1f mm Wind: %.1f m/s from %s `,
|
2024-09-29 19:46:34 +00:00
|
|
|
n.Location,
|
2024-10-03 20:48:50 +00:00
|
|
|
itemTime.Format("Monday 2 Jan 15:04"),
|
2024-09-29 19:46:34 +00:00
|
|
|
n.Temperature,
|
|
|
|
n.Percipitation,
|
2024-10-02 17:04:01 +00:00
|
|
|
n.Wind.Speed,
|
|
|
|
n.Wind.DirectionToString(),
|
2024-09-29 19:46:34 +00:00
|
|
|
)
|
2024-09-29 17:20:59 +00:00
|
|
|
}
|