chore: Add location

Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
Tim Hårek Andreassen 2024-09-29 19:50:45 +02:00
parent 04d97c2d00
commit d59a69212e
No known key found for this signature in database
GPG key ID: E59C7734F0E10EB5
3 changed files with 27 additions and 18 deletions

View file

@ -28,30 +28,23 @@ func init() {
func now(cmd *cobra.Command, args []string) { func now(cmd *cobra.Command, args []string) {
isJson, err := cmd.Flags().GetBool("json") isJson, err := cmd.Flags().GetBool("json")
check(err) cobra.CheckErr(err)
location := args[0] location := args[0]
yr, err := yr.New() yr, err := yr.New()
check(err) cobra.CheckErr(err)
now, err := yr.Now(location) now, err := yr.Now(location)
check(err) cobra.CheckErr(err)
if isJson { if isJson {
j, err := json.MarshalIndent(now, "", " ") j, err := json.MarshalIndent(now, "", " ")
check(err) cobra.CheckErr(err)
fmt.Printf("%s", j) fmt.Printf("%s", j)
return return
} }
fmt.Printf(`Current forecast fmt.Printf(`Current forecast for %s
Temperature: %.4f Temperature: %.1f
Rain: %.4f`, now.Temperature, now.Percipitation) Rain: %.1f`, now.Location, now.Temperature, now.Percipitation)
}
func check(err error) {
if err != nil {
panic(err)
}
} }

View file

@ -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) r, err := n.Search(q)
if err != nil { if err != nil {
@ -79,8 +84,11 @@ func (n *Nominatim) Lookup(q string) (*Coordinates, error) {
return nil, err return nil, err
} }
return &Coordinates{ return &LookupResult{
Latitude: lat, Location: *firstResult.Name,
Longitude: lon, Coordinates: Coordinates{
Latitude: lat,
Longitude: lon,
},
}, nil }, nil
} }

View file

@ -29,6 +29,7 @@ func New() (*Client, error) {
} }
type NowForecast struct { type NowForecast struct {
nominatim.LookupResult
Temperature float64 `json:"temperature"` Temperature float64 `json:"temperature"`
Percipitation float64 `json:"percipitation"` Percipitation float64 `json:"percipitation"`
} }
@ -55,6 +56,13 @@ func (c *Client) Now(q string) (*NowForecast, error) {
latest := ts[0].Data latest := ts[0].Data
return &NowForecast{ return &NowForecast{
LookupResult: nominatim.LookupResult{
Location: coords.Location,
Coordinates: nominatim.Coordinates{
Latitude: coords.Latitude,
Longitude: coords.Longitude,
},
},
Temperature: latest.Instant.Details.AirTemperature, Temperature: latest.Instant.Details.AirTemperature,
Percipitation: latest.Next1_Hours.Details.PrecipitationAmount, Percipitation: latest.Next1_Hours.Details.PrecipitationAmount,
}, nil }, nil