feat: Add coordinates to CLI

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-09-29 21:46:34 +02:00
parent 91ab81759f
commit d7d8ce6cd9
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
4 changed files with 55 additions and 11 deletions

View file

@ -3,7 +3,9 @@ package cmd
import (
"encoding/json"
"fmt"
"os"
"git.sr.ht/~timharek/yr-go/internal/nominatim"
"git.sr.ht/~timharek/yr-go/yr"
"github.com/spf13/cobra"
)
@ -12,7 +14,7 @@ var nowCmd = &cobra.Command{
Use: "now <location>",
Aliases: []string{"current", "n"},
Short: "Get current forecasted weather",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Run: now,
}
@ -23,22 +25,41 @@ func init() {
func now(cmd *cobra.Command, args []string) {
isJson, err := cmd.Flags().GetBool("json")
cobra.CheckErr(err)
location := args[0]
lon, _ := cmd.Flags().GetFloat64("lon")
lat, _ := cmd.Flags().GetFloat64("lat")
yr, err := yr.New()
c, err := yr.New()
cobra.CheckErr(err)
now, err := yr.Now(location)
cobra.CheckErr(err)
if len(args) == 0 && (lon == 0 || lat == 0) {
fmt.Fprintln(os.Stderr, "No location or coordinates provided.")
os.Exit(1)
}
n := &yr.NowForecast{}
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)
}
if isJson {
j, err := json.MarshalIndent(now, "", " ")
j, err := json.MarshalIndent(n, "", " ")
cobra.CheckErr(err)
fmt.Printf("%s", j)
return
}
fmt.Printf(`Current forecast for %s
fmt.Printf(
`Current forecast for %s
Temperature: %.1f
Rain: %.1f`, now.Location, now.Temperature, now.Percipitation)
Rain: %.1f`,
n.Location,
n.Temperature,
n.Percipitation,
)
}

View file

@ -21,4 +21,6 @@ func Execute() {
func init() {
rootCmd.PersistentFlags().Bool("json", false, "Result in JSON")
rootCmd.PersistentFlags().Float64P("lon", "x", 0, "Longitude coordinate")
rootCmd.PersistentFlags().Float64P("lat", "y", 0, "Latitude coordinate")
}

6
go.mod
View file

@ -2,7 +2,10 @@ module git.sr.ht/~timharek/yr-go
go 1.23.1
require github.com/stretchr/testify v1.9.0
require (
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
)
require (
github.com/davecgh/go-spew v1.1.1 // indirect
@ -10,7 +13,6 @@ require (
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

View file

@ -40,11 +40,30 @@ func (c *Client) Now(q string) (*NowForecast, error) {
return nil, err
}
return c.NowCoords(&nominatim.Coordinates{
Latitude: coords.Latitude,
Longitude: coords.Longitude,
}, &coords.Location)
}
func (c *Client) NowCoords(coords *nominatim.Coordinates, location *string) (*NowForecast, error) {
f, err := c.met.Forecast(coords.Latitude, coords.Longitude, nil)
if err != nil {
return nil, err
}
if location == nil {
l, err := c.nom.ReverseLookup(coords.Latitude, coords.Longitude)
if err != nil {
return nil, err
}
location = &l.Location
}
if location == nil {
return nil, fmt.Errorf("Location name is not set")
}
ts := f.Properties.Timeseries
if len(ts) == 0 {
@ -57,7 +76,7 @@ func (c *Client) Now(q string) (*NowForecast, error) {
return &NowForecast{
LookupResult: nominatim.LookupResult{
Location: coords.Location,
Location: *location,
Coordinates: nominatim.Coordinates{
Latitude: coords.Latitude,
Longitude: coords.Longitude,