diff --git a/src/segments/owm.go b/src/segments/owm.go index d446cec029e6..694b8401ee9b 100644 --- a/src/segments/owm.go +++ b/src/segments/owm.go @@ -33,8 +33,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 + PoshOWMAPIKey string = "POSH_OWM_API_KEY" + // Environmental variable to dynamically set the location string + PoshOWMLocationKey string = "POSH_OWM_LOCATION" ) type weather struct { @@ -88,14 +90,21 @@ func (d *Owm) getResult() (*owmDataResponse, error) { apikey = d.env.Getenv(PoshOWMAPIKey) } - location := d.props.GetString(Location, "De Bilt,NL") + if len(apikey) == 0 { + return nil, errors.New("no api key found") + } - location = url.QueryEscape(location) + location := d.props.GetString(Location, "De Bilt,NL") + if len(location) == 0 { + location = d.env.Getenv(PoshOWMLocationKey) + } - if len(apikey) == 0 || len(location) == 0 { - return nil, errors.New("no api key or location found") + if len(location) == 0 { + return nil, errors.New("no location found") } + location = url.QueryEscape(location) + units := d.props.GetString(Units, "standard") httpTimeout := d.props.GetInt(properties.HTTPTimeout, properties.DefaultHTTPTimeout) diff --git a/src/segments/owm_test.go b/src/segments/owm_test.go index 8a269eb8b310..337880545007 100644 --- a/src/segments/owm_test.go +++ b/src/segments/owm_test.go @@ -85,6 +85,7 @@ func TestOWMSegmentSingle(t *testing.T) { testURL := fmt.Sprintf(OWMWEATHERAPIURL, location) env.On("HTTPRequest", testURL).Return([]byte(tc.WeatherJSONResponse), tc.Error) env.On("Error", testify_.Anything) + env.On("Getenv", "POSH_OWM_LOCATION").Return("") o := &Owm{ props: props, diff --git a/website/docs/segments/web/owm.mdx b/website/docs/segments/web/owm.mdx index be9ea8e99084..18a07807621f 100644 --- a/website/docs/segments/web/owm.mdx +++ b/website/docs/segments/web/owm.mdx @@ -37,13 +37,13 @@ import Config from "@site/src/components/Config.js"; ## 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 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 | -| `cache_timeout` | `int` | `10` | in minutes, the timeout for request caching. A value of 0 disables the cache. | +| 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 if the property is set to an empty string (`""`) | +| `location` | `string` | `De Bilt,NL` | The requested location interpreted only if valid coordinates aren't given. Formatted as \. 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 if the property is set to an empty string (`""`) | +| `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 | +| `cache_timeout` | `int` | `10` | in minutes, the timeout for request caching. A value of 0 disables the cache. | ## Template ([info][templates])