From 74da88f99cda6f58cbe5cd94b18f36abca60402d Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Wed, 29 Nov 2023 11:10:09 +0100 Subject: [PATCH] feat(owm): add API key environment variable resolves #4488 --- src/segments/owm.go | 10 ++++++++++ website/docs/segments/owm.mdx | 18 +++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/segments/owm.go b/src/segments/owm.go index 9572c5c1..bb2b36ac 100644 --- a/src/segments/owm.go +++ b/src/segments/owm.go @@ -36,6 +36,8 @@ 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" ) type weather struct { @@ -90,6 +92,14 @@ func (d *Owm) getResult() (*owmDataResponse, error) { } apikey := properties.OneOf[string](d.props, ".", APIKey, "apiKey") + if len(apikey) == 0 { + apikey = d.env.Getenv(PoshOWMAPIKey) + } + + if len(apikey) == 0 { + return nil, errors.New("no api key found") + } + location := d.props.GetString(Location, "De Bilt,NL") latitude := d.props.GetFloat64(Latitude, 91) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value longitude := d.props.GetFloat64(Longitude, 181) // This default value is intentionally invalid since there should not be a default for this and 0 is a valid value diff --git a/website/docs/segments/owm.mdx b/website/docs/segments/owm.mdx index 6df3d8f5..7e75f715 100644 --- a/website/docs/segments/owm.mdx +++ b/website/docs/segments/owm.mdx @@ -37,16 +37,16 @@ import Config from "@site/src/components/Config.js"; ## Properties -| Name | Type | Description | -| --------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `api_key` | `string` | Your API key from [Open Weather Map][owm] | -| `latitude` | `float64` | The latitude of the requested location. | -| `longitude` | `float64` | The longitude of the requested location. | +| Name | Type | Description | +| --------------- | --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `api_key` | `string` | Your API key from [Open Weather Map][owm]. Can also be set using the `POSH_OWM_API_KEY` environment variable. | +| `latitude` | `float64` | The latitude of the requested location. | +| `longitude` | `float64` | The longitude of the requested location. | | `location` | `string` | The requested location. Formatted as \. City name, state code and country code divided by comma. Please, refer to ISO 3166 for the state codes or country codes - defaults to `DE BILT,NL` | -| `units` | `string` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) - defaults to `standard` | -| `http_timeout` | `int` | The default timeout for http request is 20ms. | -| `cache_timeout` | `int` | The default timeout for request caching is 10m. A value of 0 disables the cache. | -| `template` | `string` | A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the properties below - defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})` | +| `units` | `string` | Units of measurement. Available values are standard (kelvin), metric (celsius), and imperial (fahrenheit) - defaults to `standard` | +| `http_timeout` | `int` | The default timeout for http request is 20ms. | +| `cache_timeout` | `int` | The default timeout for request caching is 10m. A value of 0 disables the cache. | +| `template` | `string` | A go [text/template][go-text-template] template extended with [sprig][sprig] utilizing the properties below - defaults to `{{.Weather}} ({{.Temperature}}{{.UnitIcon}})` | ### Specifying Location