diff options
| author | Paul Buetow <paul@buetow.org> | 2024-06-18 10:04:20 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-06-18 10:04:20 +0300 |
| commit | 9b12909a5b169000135137a96c37bcd7ea2ff70e (patch) | |
| tree | c9858d1b0daaf313141a7a64f9a88cb71b7d39f4 /internal/config | |
| parent | c8d842353f1b21971443dcaf6658bad77ad24b85 (diff) | |
can open EDITOR
Diffstat (limited to 'internal/config')
| -rw-r--r-- | internal/config/config.go | 28 | ||||
| -rw-r--r-- | internal/config/config_test.go | 23 |
2 files changed, 38 insertions, 13 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 784c34b..c05d553 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io" "os" + "unicode" ) func FromFile[T any](configFile string) (T, error) { @@ -25,17 +26,28 @@ func FromFile[T any](configFile string) (T, error) { } // Set config from envoronment variable if present, e.g. hansWurst from GOS_HANS_WURST -func FromENV(envKey string, defaultValue ...string) string { - if value := os.Getenv(envKey); value != "" { - return value - } - - // Use first non-empty default value. - for _, value := range defaultValue { - if value != "" { +func FromENV(keys ...string) string { + for _, key := range keys{ + if key == "" { + continue + } + if !isAllUpperCase(key) { + return key + } + if value := os.Getenv(key); value != "" { return value } } return "" } + +func isAllUpperCase(s string) bool { + for _, r := range s { + if unicode.IsLetter(r) && !unicode.IsUpper(r) { + return false + } + } + return true +} + diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 2bd8fb6..a0a5228 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -17,7 +17,6 @@ func TestFromENV(t *testing.T) { if got != expected { t.Errorf("got '%s' but expected '%s'", got, expected) - return } t.Logf("got '%s' as expected", expected) @@ -25,13 +24,12 @@ func TestFromENV(t *testing.T) { got = FromENV("GOS_JAJAJA", expected) if got != expected { t.Errorf("got '%s' but expected '%s'", got, expected) - return } t.Logf("got '%s' as expected", expected) - if got = FromENV("jujuju"); got != "" { + os.Unsetenv("JUJUJU_NOT_EXISTANT_ENV") + if got = FromENV("JUJUJU_NOT_EXISTANT_ENV"); got != "" { t.Errorf("got '%s' but expected empty string", got) - return } t.Logf("got empty string as expected") @@ -39,7 +37,22 @@ func TestFromENV(t *testing.T) { got = FromENV("GOS_WATCH", "", "", "", expected, "") if got != expected { t.Errorf("got '%s' but expected '%s'", got, expected) - return } t.Logf("got '%s' as expected", expected) } + +func TestSecondENV(t *testing.T) { + t.Parallel() + + os.Unsetenv("GOS_NONEXISTANT") + os.Setenv("EDITOR", "hx") + + var ( + expected = "hx" + got = FromENV("GOS_NONEXISTANT", "EDITOR", "notepad.exe") + ) + + if expected != got { + t.Errorf("got '%s' but expected '%s'", got, expected) + } +} |
