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) {
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)
}

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

View file

@ -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