2024-09-29 17:20:59 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-09-29 19:46:34 +00:00
|
|
|
"os"
|
2024-10-02 19:04:58 +00:00
|
|
|
"time"
|
2024-09-29 17:20:59 +00:00
|
|
|
|
2024-10-03 16:53:14 +00:00
|
|
|
"git.sr.ht/~timharek/yr/cmd/flags"
|
|
|
|
"git.sr.ht/~timharek/yr/internal/nominatim"
|
|
|
|
"git.sr.ht/~timharek/yr/yr"
|
2024-10-03 14:05:18 +00:00
|
|
|
"github.com/pkg/browser"
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func now(cmd *cobra.Command, args []string) {
|
2024-10-02 13:47:49 +00:00
|
|
|
isJson, err := cmd.Flags().GetBool(flags.JSON)
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-10-02 19:04:58 +00:00
|
|
|
isUTC, err := cmd.Flags().GetBool(flags.UTC)
|
|
|
|
cobra.CheckErr(err)
|
2024-10-03 14:05:18 +00:00
|
|
|
isWeb, err := cmd.Flags().GetBool(flags.WEB)
|
|
|
|
cobra.CheckErr(err)
|
2024-10-02 13:47:49 +00:00
|
|
|
lon, _ := cmd.Flags().GetFloat64(flags.LON)
|
|
|
|
lat, _ := cmd.Flags().GetFloat64(flags.LAT)
|
2024-09-29 17:20:59 +00:00
|
|
|
|
2024-09-29 19:46:34 +00:00
|
|
|
c, err := yr.New()
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-09-29 17:20:59 +00:00
|
|
|
|
2024-09-29 19:46:34 +00:00
|
|
|
if len(args) == 0 && (lon == 0 || lat == 0) {
|
|
|
|
fmt.Fprintln(os.Stderr, "No location or coordinates provided.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-10-03 14:05:18 +00:00
|
|
|
f := &yr.ForecastResult{}
|
2024-09-29 19:46:34 +00:00
|
|
|
if len(args) == 0 {
|
2024-10-03 14:05:18 +00:00
|
|
|
f, err = c.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil)
|
2024-09-29 19:46:34 +00:00
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
location := args[0]
|
2024-10-03 14:05:18 +00:00
|
|
|
f, err = c.Now(location)
|
2024-09-29 19:46:34 +00:00
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
}
|
2024-09-29 17:20:59 +00:00
|
|
|
|
2024-10-03 14:05:18 +00:00
|
|
|
if isWeb {
|
|
|
|
url := fmt.Sprintf("https://www.yr.no/en/forecast/hourly-table/%.4f,%.4f/?i=0", f.Coordinates.Latitude, f.Coordinates.Longitude)
|
|
|
|
err := browser.OpenURL(url)
|
|
|
|
if err != nil {
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := f.Forecast[0]
|
|
|
|
|
2024-09-29 17:20:59 +00:00
|
|
|
if isJson {
|
2024-10-03 14:05:18 +00:00
|
|
|
j, err := json.MarshalIndent(f, "", " ")
|
2024-09-29 17:50:45 +00:00
|
|
|
cobra.CheckErr(err)
|
2024-09-29 17:20:59 +00:00
|
|
|
fmt.Printf("%s", j)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-02 19:04:58 +00:00
|
|
|
itemTime := n.Time.Local()
|
|
|
|
if isUTC {
|
|
|
|
itemTime = n.Time
|
|
|
|
}
|
2024-09-29 19:46:34 +00:00
|
|
|
fmt.Printf(
|
2024-10-02 19:04:58 +00:00
|
|
|
`%s now:
|
|
|
|
%s
|
2024-10-02 17:22:44 +00:00
|
|
|
Temperature: %.1f °C
|
|
|
|
Rain: %.1f mm
|
2024-10-02 19:04:58 +00:00
|
|
|
Wind: %.1f m/s from %s
|
|
|
|
`,
|
2024-09-29 19:46:34 +00:00
|
|
|
n.Location,
|
2024-10-02 19:04:58 +00:00
|
|
|
itemTime.Format(time.DateTime),
|
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
|
|
|
}
|