mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor: use the same string for API key properties
This commit is contained in:
parent
cf3dc7c069
commit
1298129b87
|
@ -15,6 +15,7 @@ type Properties interface {
|
||||||
GetInt(property Property, defaultValue int) int
|
GetInt(property Property, defaultValue int) int
|
||||||
GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string
|
GetKeyValueMap(property Property, defaultValue map[string]string) map[string]string
|
||||||
GetStringArray(property Property, defaultValue []string) []string
|
GetStringArray(property Property, defaultValue []string) []string
|
||||||
|
Get(property Property, defaultValue any) any
|
||||||
}
|
}
|
||||||
|
|
||||||
// Property defines one property of a segment for context
|
// Property defines one property of a segment for context
|
||||||
|
@ -158,6 +159,15 @@ func (m Map) GetStringArray(property Property, defaultValue []string) []string {
|
||||||
return keyValues
|
return keyValues
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m Map) Get(property Property, defaultValue any) any {
|
||||||
|
val, found := m[property]
|
||||||
|
if !found {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
||||||
|
return val
|
||||||
|
}
|
||||||
|
|
||||||
func ParseStringArray(param any) []string {
|
func ParseStringArray(param any) []string {
|
||||||
switch v := param.(type) {
|
switch v := param.(type) {
|
||||||
default:
|
default:
|
||||||
|
@ -207,3 +217,25 @@ func parseKeyValueArray(param any) map[string]string {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generic functions
|
||||||
|
|
||||||
|
type Value interface {
|
||||||
|
string | int | []string | float64 | bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func OneOf[T Value](properties Properties, defaultValue T, props ...Property) T {
|
||||||
|
for _, prop := range props {
|
||||||
|
// get value on a generic get, then see if we can cast to T?
|
||||||
|
val := properties.Get(prop, nil)
|
||||||
|
if val == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, ok := val.(T); ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
|
|
@ -111,3 +111,57 @@ func TestGetFloat64PropertyNotInMap(t *testing.T) {
|
||||||
value := properties.GetFloat64(Foo, expected)
|
value := properties.GetFloat64(Foo, expected)
|
||||||
assert.Equal(t, expected, value)
|
assert.Equal(t, expected, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOneOf(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
Map Map
|
||||||
|
Properties []Property
|
||||||
|
DefaultValue string
|
||||||
|
Expected any
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
Case: "one element",
|
||||||
|
Expected: "1337",
|
||||||
|
Properties: []Property{Foo},
|
||||||
|
Map: Map{
|
||||||
|
Foo: "1337",
|
||||||
|
},
|
||||||
|
DefaultValue: "2000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "two elements",
|
||||||
|
Expected: "1337",
|
||||||
|
Properties: []Property{Foo},
|
||||||
|
Map: Map{
|
||||||
|
Foo: "1337",
|
||||||
|
"Bar": "9001",
|
||||||
|
},
|
||||||
|
DefaultValue: "2000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "no match",
|
||||||
|
Expected: "2000",
|
||||||
|
Properties: []Property{"Moo"},
|
||||||
|
Map: Map{
|
||||||
|
Foo: "1337",
|
||||||
|
"Bar": "9001",
|
||||||
|
},
|
||||||
|
DefaultValue: "2000",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Case: "incorrect type",
|
||||||
|
Expected: "2000",
|
||||||
|
Properties: []Property{Foo},
|
||||||
|
Map: Map{
|
||||||
|
Foo: 1337,
|
||||||
|
"Bar": "9001",
|
||||||
|
},
|
||||||
|
DefaultValue: "2000",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
value := OneOf(tc.Map, tc.DefaultValue, tc.Properties...)
|
||||||
|
assert.Equal(t, tc.Expected, value, tc.Case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -52,3 +52,9 @@ func (w *Wrapper) GetStringArray(property Property, defaultValue []string) []str
|
||||||
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
|
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Wrapper) Get(property Property, defaultValue any) any {
|
||||||
|
value := w.Properties.Get(property, defaultValue)
|
||||||
|
w.Env.Debug(fmt.Sprintf("%s: %v", property, value))
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
|
@ -34,7 +34,6 @@ type Brewfather struct {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BFUserID properties.Property = "user_id"
|
BFUserID properties.Property = "user_id"
|
||||||
BFAPIKey properties.Property = "api_key"
|
|
||||||
BFBatchID properties.Property = "batch_id"
|
BFBatchID properties.Property = "batch_id"
|
||||||
|
|
||||||
BFDoubleUpIcon properties.Property = "doubleup_icon"
|
BFDoubleUpIcon properties.Property = "doubleup_icon"
|
||||||
|
@ -228,7 +227,7 @@ func (bf *Brewfather) getResult() (*Batch, error) {
|
||||||
return nil, errors.New("missing Brewfather user id (user_id)")
|
return nil, errors.New("missing Brewfather user id (user_id)")
|
||||||
}
|
}
|
||||||
|
|
||||||
apiKey := bf.props.GetString(BFAPIKey, "")
|
apiKey := bf.props.GetString(APIKey, "")
|
||||||
if len(apiKey) == 0 {
|
if len(apiKey) == 0 {
|
||||||
return nil, errors.New("missing Brewfather api key (api_key)")
|
return nil, errors.New("missing Brewfather api key (api_key)")
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,7 +148,7 @@ func TestBrewfatherSegment(t *testing.T) {
|
||||||
props := properties.Map{
|
props := properties.Map{
|
||||||
properties.CacheTimeout: tc.CacheTimeout,
|
properties.CacheTimeout: tc.CacheTimeout,
|
||||||
BFBatchID: BFFakeBatchID,
|
BFBatchID: BFFakeBatchID,
|
||||||
BFAPIKey: "FAKE",
|
APIKey: "FAKE",
|
||||||
BFUserID: "FAKE",
|
BFUserID: "FAKE",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ type Owm struct {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// APIKey openweathermap api key
|
// APIKey openweathermap api key
|
||||||
APIKey properties.Property = "apikey"
|
APIKey properties.Property = "api_key"
|
||||||
// Location openweathermap location
|
// Location openweathermap location
|
||||||
Location properties.Property = "location"
|
Location properties.Property = "location"
|
||||||
// Units openweathermap units
|
// Units openweathermap units
|
||||||
|
@ -89,7 +89,7 @@ func (d *Owm) getResult() (*owmDataResponse, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
apikey := d.props.GetString(APIKey, ".")
|
apikey := properties.OneOf[string](d.props, ".", APIKey, "apiKey")
|
||||||
location := d.props.GetString(Location, "De Bilt,NL")
|
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
|
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
|
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
|
||||||
|
|
|
@ -2280,16 +2280,16 @@
|
||||||
"properties": {
|
"properties": {
|
||||||
"properties": {
|
"properties": {
|
||||||
"properties": {
|
"properties": {
|
||||||
"apikey": {
|
"api_key": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title": "apikey",
|
"title": "API key",
|
||||||
"description": "The apikey used for the api call (Required)",
|
"description": "The API key used for the api call (Required)",
|
||||||
"default": "."
|
"default": "."
|
||||||
},
|
},
|
||||||
"location": {
|
"location": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title": "location",
|
"title": "location",
|
||||||
"description": "Location to use for the api call. 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.",
|
"description": "Location to use for the API call. 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.",
|
||||||
"default": "De Bilt,NL"
|
"default": "De Bilt,NL"
|
||||||
},
|
},
|
||||||
"units": {
|
"units": {
|
||||||
|
@ -3182,16 +3182,16 @@
|
||||||
"description": "Text/icon to show when stopped",
|
"description": "Text/icon to show when stopped",
|
||||||
"default": "\uF04D"
|
"default": "\uF04D"
|
||||||
},
|
},
|
||||||
"apiKey": {
|
"api_key": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title": "apiKey",
|
"title": "API key",
|
||||||
"description": "The apikey used for the api call (Required)",
|
"description": "The API key used for the API call (Required)",
|
||||||
"default": "."
|
"default": "."
|
||||||
},
|
},
|
||||||
"username": {
|
"username": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"title": "username",
|
"title": "username",
|
||||||
"description": "The username used for the api call (Required)",
|
"description": "The username used for the API call (Required)",
|
||||||
"default": "."
|
"default": "."
|
||||||
},
|
},
|
||||||
"http_timeout": {
|
"http_timeout": {
|
||||||
|
|
|
@ -19,22 +19,24 @@ You **must** request an [API key][api-key] at the LastFM website.
|
||||||
|
|
||||||
## Sample Configuration
|
## Sample Configuration
|
||||||
|
|
||||||
import Config from '@site/src/components/Config.js';
|
import Config from "@site/src/components/Config.js";
|
||||||
|
|
||||||
<Config data={{
|
<Config
|
||||||
"background": "p:sky",
|
data={{
|
||||||
"foreground": "p:white",
|
background: "p:sky",
|
||||||
"powerline_symbol": "\ue0b0",
|
foreground: "p:white",
|
||||||
"properties": {
|
powerline_symbol: "\ue0b0",
|
||||||
"apikey": "<YOUR_API_KEY>",
|
properties: {
|
||||||
"username": "<LASTFM_USERNAME>",
|
api_key: "<YOUR_API_KEY>",
|
||||||
"http_timeout": 20000,
|
username: "<LASTFM_USERNAME>",
|
||||||
"cache_timeout": 1
|
http_timeout: 20000,
|
||||||
},
|
cache_timeout: 1,
|
||||||
"style": "powerline",
|
},
|
||||||
"template": " {{ .Icon }}{{ if ne .Status \"stopped\" }}{{ .Full }}{{ end }} ",
|
style: "powerline",
|
||||||
"type": "lastfm"
|
template: ' {{ .Icon }}{{ if ne .Status "stopped" }}{{ .Full }}{{ end }} ',
|
||||||
}}/>
|
type: "lastfm",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
|
@ -42,7 +44,7 @@ import Config from '@site/src/components/Config.js';
|
||||||
| -------------- | -------- | ------------------------------------------------------ |
|
| -------------- | -------- | ------------------------------------------------------ |
|
||||||
| `playing_icon` | `string` | text/icon to show when playing - defaults to `\uE602 ` |
|
| `playing_icon` | `string` | text/icon to show when playing - defaults to `\uE602 ` |
|
||||||
| `stopped_icon` | `string` | text/icon to show when stopped - defaults to `\uF04D ` |
|
| `stopped_icon` | `string` | text/icon to show when stopped - defaults to `\uF04D ` |
|
||||||
| `apikey` | `string` | your LastFM [API key][api-key] |
|
| `api_key` | `string` | your LastFM [API key][api-key] |
|
||||||
| `username` | `string` | your LastFM username |
|
| `username` | `string` | your LastFM username |
|
||||||
|
|
||||||
## Template ([info][templates])
|
## Template ([info][templates])
|
||||||
|
|
|
@ -15,29 +15,31 @@ The free tier for _Current weather and forecasts collection_ is sufficient.
|
||||||
|
|
||||||
## Sample Configuration
|
## Sample Configuration
|
||||||
|
|
||||||
import Config from '@site/src/components/Config.js';
|
import Config from "@site/src/components/Config.js";
|
||||||
|
|
||||||
<Config data={{
|
<Config
|
||||||
"type": "owm",
|
data={{
|
||||||
"style": "powerline",
|
type: "owm",
|
||||||
"powerline_symbol": "\uE0B0",
|
style: "powerline",
|
||||||
"foreground": "#ffffff",
|
powerline_symbol: "\uE0B0",
|
||||||
"background": "#FF0000",
|
foreground: "#ffffff",
|
||||||
"template": "{{.Weather}} ({{.Temperature}}{{.UnitIcon}})",
|
background: "#FF0000",
|
||||||
"properties": {
|
template: "{{.Weather}} ({{.Temperature}}{{.UnitIcon}})",
|
||||||
"apikey": "<YOUR_API_KEY>",
|
properties: {
|
||||||
"location": "AMSTERDAM,NL",
|
api_key: "<YOUR_API_KEY>",
|
||||||
"units": "metric",
|
location: "AMSTERDAM,NL",
|
||||||
"http_timeout": 20,
|
units: "metric",
|
||||||
"cache_timeout": 10
|
http_timeout: 20,
|
||||||
}
|
cache_timeout: 10,
|
||||||
}}/>
|
},
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
## Properties
|
## Properties
|
||||||
|
|
||||||
| Name | Type | Description |
|
| Name | Type | Description |
|
||||||
| --------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| --------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||||
| `apikey` | `string` | Your API key from [Open Weather Map][owm] |
|
| `api_key` | `string` | Your API key from [Open Weather Map][owm] |
|
||||||
| `latitude` | `float64` | The latitude of the requested location. |
|
| `latitude` | `float64` | The latitude of the requested location. |
|
||||||
| `longitude` | `float64` | The longitude of the requested location. |
|
| `longitude` | `float64` | The longitude of the requested location. |
|
||||||
| `location` | `string` | The requested location. 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 - defaults to `DE BILT,NL` |
|
| `location` | `string` | The requested location. 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 - defaults to `DE BILT,NL` |
|
||||||
|
|
Loading…
Reference in a new issue