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) {
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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{
|
||||||
|
Location: *firstResult.Name,
|
||||||
|
Coordinates: Coordinates{
|
||||||
Latitude: lat,
|
Latitude: lat,
|
||||||
Longitude: lon,
|
Longitude: lon,
|
||||||
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
8
yr/yr.go
8
yr/yr.go
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue