chore: Add location
Signed-off-by: Tim Hårek Andreassen <tim@harek.no>
This commit is contained in:
parent
04d97c2d00
commit
d59a69212e
3 changed files with 27 additions and 18 deletions
21
cmd/now.go
21
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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
8
yr/yr.go
8
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
|
||||
|
|
Loading…
Reference in a new issue