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-02 13:47:49 +00:00
|
|
|
"git.sr.ht/~timharek/yr-go/cmd/flags"
|
2024-09-29 19:46:34 +00:00
|
|
|
"git.sr.ht/~timharek/yr-go/internal/nominatim"
|
2024-09-29 17:20:59 +00:00
|
|
|
"git.sr.ht/~timharek/yr-go/yr"
|
|
|
|
"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-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-02 17:51:44 +00:00
|
|
|
n := &yr.Forecast{}
|
2024-09-29 19:46:34 +00:00
|
|
|
if len(args) == 0 {
|
|
|
|
n, err = c.NowCoords(&nominatim.Coordinates{Longitude: lon, Latitude: lat}, nil)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
location := args[0]
|
|
|
|
n, err = c.Now(location)
|
|
|
|
cobra.CheckErr(err)
|
|
|
|
|
|
|
|
}
|
2024-09-29 17:20:59 +00:00
|
|
|
|
|
|
|
if isJson {
|
2024-09-29 19:46:34 +00:00
|
|
|
j, err := json.MarshalIndent(n, "", " ")
|
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
|
|
|
}
|