From d7d8ce6cd96e7824547ca5d0f56f420f33029f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20H=C3=A5rek=20Andreassen?= Date: Sun, 29 Sep 2024 21:46:34 +0200 Subject: [PATCH] feat: Add coordinates to CLI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tim HĂ„rek Andreassen --- cmd/now.go | 37 +++++++++++++++++++++++++++++-------- cmd/root.go | 2 ++ go.mod | 6 ++++-- yr/yr.go | 21 ++++++++++++++++++++- 4 files changed, 55 insertions(+), 11 deletions(-) diff --git a/cmd/now.go b/cmd/now.go index 7c2aaa3..ccab052 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -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 ", 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, + ) } diff --git a/cmd/root.go b/cmd/root.go index a3ea5a6..06fc9bd 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -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") } diff --git a/go.mod b/go.mod index 5915f39..c910b15 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/yr/yr.go b/yr/yr.go index f42292c..fea2300 100644 --- a/yr/yr.go +++ b/yr/yr.go @@ -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,