diff --git a/cmd/now.go b/cmd/now.go index ac1bc1e..a84bfb3 100644 --- a/cmd/now.go +++ b/cmd/now.go @@ -28,30 +28,23 @@ func init() { func now(cmd *cobra.Command, args []string) { isJson, err := cmd.Flags().GetBool("json") - check(err) + cobra.CheckErr(err) location := args[0] yr, err := yr.New() - check(err) + cobra.CheckErr(err) now, err := yr.Now(location) - check(err) + cobra.CheckErr(err) if isJson { j, err := json.MarshalIndent(now, "", " ") - check(err) + cobra.CheckErr(err) fmt.Printf("%s", j) return } - fmt.Printf(`Current forecast - Temperature: %.4f - Rain: %.4f`, now.Temperature, now.Percipitation) - -} - -func check(err error) { - if err != nil { - panic(err) - } + fmt.Printf(`Current forecast for %s + Temperature: %.1f + Rain: %.1f`, now.Location, now.Temperature, now.Percipitation) } diff --git a/internal/nominatim/nominatim.go b/internal/nominatim/nominatim.go index 33d81f8..618420a 100644 --- a/internal/nominatim/nominatim.go +++ b/internal/nominatim/nominatim.go @@ -57,7 +57,12 @@ func (n *Nominatim) Search(q string) (*SearchResults, error) { } -func (n *Nominatim) Lookup(q string) (*Coordinates, error) { +type LookupResult struct { + Coordinates + Location string `json:"location"` +} + +func (n *Nominatim) Lookup(q string) (*LookupResult, error) { r, err := n.Search(q) if err != nil { @@ -79,8 +84,11 @@ func (n *Nominatim) Lookup(q string) (*Coordinates, error) { return nil, err } - return &Coordinates{ - Latitude: lat, - Longitude: lon, + return &LookupResult{ + Location: *firstResult.Name, + Coordinates: Coordinates{ + Latitude: lat, + Longitude: lon, + }, }, nil } diff --git a/yr/yr.go b/yr/yr.go index aeb93e2..f42292c 100644 --- a/yr/yr.go +++ b/yr/yr.go @@ -29,6 +29,7 @@ func New() (*Client, error) { } type NowForecast struct { + nominatim.LookupResult Temperature float64 `json:"temperature"` Percipitation float64 `json:"percipitation"` } @@ -55,6 +56,13 @@ func (c *Client) Now(q string) (*NowForecast, error) { latest := ts[0].Data return &NowForecast{ + LookupResult: nominatim.LookupResult{ + Location: coords.Location, + Coordinates: nominatim.Coordinates{ + Latitude: coords.Latitude, + Longitude: coords.Longitude, + }, + }, Temperature: latest.Instant.Details.AirTemperature, Percipitation: latest.Next1_Hours.Details.PrecipitationAmount, }, nil