mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat(owm): add ability to set location by env var
This commit is contained in:
parent
82f8a71533
commit
58670c5a80
|
@ -32,8 +32,10 @@ const (
|
|||
CacheKeyResponse string = "owm_response"
|
||||
// CacheKeyURL key used when caching the url responsible for the response
|
||||
CacheKeyURL string = "owm_url"
|
||||
|
||||
PoshOWMAPIKey = "POSH_OWM_API_KEY"
|
||||
// Environmental variable to dynamically set the Open Map API key
|
||||
OWMAPIKey string = "POSH_OWM_API_KEY"
|
||||
// Environmental variable to dynamically set the location string
|
||||
OWMLocationKey string = "POSH_OWM_LOCATION"
|
||||
)
|
||||
|
||||
type weather struct {
|
||||
|
@ -68,18 +70,22 @@ func (d *Owm) Template() string {
|
|||
func (d *Owm) getResult() (*owmDataResponse, error) {
|
||||
response := new(owmDataResponse)
|
||||
|
||||
apikey := properties.OneOf(d.props, ".", APIKey, "apiKey")
|
||||
apikey := properties.OneOf(d.props, d.env.Getenv(OWMAPIKey), APIKey, "apiKey")
|
||||
if len(apikey) == 0 {
|
||||
apikey = d.env.Getenv(PoshOWMAPIKey)
|
||||
apikey = "."
|
||||
}
|
||||
|
||||
if len(apikey) == 0 {
|
||||
return nil, errors.New("no api key found")
|
||||
}
|
||||
|
||||
location := d.props.GetString(Location, d.env.Getenv(OWMLocationKey))
|
||||
if len(location) == 0 {
|
||||
return nil, errors.New("no location found")
|
||||
}
|
||||
|
||||
location := d.props.GetString(Location, "De Bilt,NL")
|
||||
location = url.QueryEscape(location)
|
||||
|
||||
if len(apikey) == 0 || len(location) == 0 {
|
||||
return nil, errors.New("no api key or location found")
|
||||
}
|
||||
|
||||
units := d.props.GetString(Units, "standard")
|
||||
httpTimeout := d.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout)
|
||||
|
||||
|
|
|
@ -81,6 +81,8 @@ func TestOWMSegmentSingle(t *testing.T) {
|
|||
location := url.QueryEscape(tc.Location)
|
||||
testURL := fmt.Sprintf(OWMWEATHERAPIURL, location)
|
||||
env.On("HTTPRequest", testURL).Return([]byte(tc.WeatherJSONResponse), tc.Error)
|
||||
env.On("Getenv", OWMLocationKey).Return("")
|
||||
env.On("Getenv", OWMAPIKey).Return("")
|
||||
|
||||
o := &Owm{}
|
||||
o.Init(props, env)
|
||||
|
@ -207,6 +209,8 @@ func TestOWMSegmentIcons(t *testing.T) {
|
|||
expectedString := fmt.Sprintf("%s (20°C)", tc.ExpectedIconString)
|
||||
|
||||
env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil)
|
||||
env.On("Getenv", OWMLocationKey).Return("")
|
||||
env.On("Getenv", OWMAPIKey).Return("")
|
||||
|
||||
props := properties.Map{
|
||||
APIKey: "key",
|
||||
|
@ -220,26 +224,4 @@ func TestOWMSegmentIcons(t *testing.T) {
|
|||
assert.Nil(t, o.setStatus())
|
||||
assert.Equal(t, expectedString, renderTemplate(env, o.Template(), o), tc.Case)
|
||||
}
|
||||
|
||||
// test with hyperlink enabled
|
||||
for _, tc := range cases {
|
||||
env := &mock.Environment{}
|
||||
|
||||
weatherResponse := fmt.Sprintf(`{"weather":[{"icon":"%s"}],"main":{"temp":20.3}}`, tc.IconID)
|
||||
expectedString := fmt.Sprintf("«%s (20°C)»(%s)", tc.ExpectedIconString, testURL)
|
||||
|
||||
env.On("HTTPRequest", testURL).Return([]byte(weatherResponse), nil)
|
||||
|
||||
props := properties.Map{
|
||||
APIKey: "key",
|
||||
Location: "AMSTERDAM,NL",
|
||||
Units: "metric",
|
||||
}
|
||||
|
||||
o := &Owm{}
|
||||
o.Init(props, env)
|
||||
|
||||
assert.Nil(t, o.setStatus())
|
||||
assert.Equal(t, expectedString, renderTemplate(env, "«{{.Weather}} ({{.Temperature}}{{.UnitIcon}})»({{.URL}})", o), tc.Case)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,17 +31,17 @@ import Config from "@site/src/components/Config.js";
|
|||
units: "metric",
|
||||
http_timeout: 20,
|
||||
},
|
||||
}}u
|
||||
}}
|
||||
/>
|
||||
|
||||
## Properties
|
||||
|
||||
| Name | Type | Default | Description |
|
||||
| -------------- | :------: | :----------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `api_key` | `string` | `.` | Your API key from [Open Weather Map][owm]. Can also be set using the `POSH_OWM_API_KEY` environment variable. |
|
||||
| `location` | `string` | `De Bilt,NL` | The requested location interpreted only if valid coordinates aren't given. Formatted as \<City,STATE,COUNTRY_CODE\>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes |
|
||||
| `units` | `string` | `standard` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) |
|
||||
| `http_timeout` | `int` | `20` | in milliseconds, the timeout for http request |
|
||||
| Name | Type | Default | Description |
|
||||
| -------------- | :------: | :----------: | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `api_key` | `string` | `.` | Your API key from [Open Weather Map][owm]. Can also be set using the `POSH_OWM_API_KEY` environment variable |
|
||||
| `location` | `string` | `De Bilt,NL` | The requested location interpreted only if valid coordinates aren't given. Formatted as \<City,STATE,COUNTRY_CODE\>. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes . Can also be set using the `POSH_OWM_LOCATION` environment variable |
|
||||
| `units` | `string` | `standard` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) |
|
||||
| `http_timeout` | `int` | `20` | in milliseconds, the timeout for http request |
|
||||
|
||||
## Template ([info][templates])
|
||||
|
||||
|
|
Loading…
Reference in a new issue