[cli] Add tooling for cli

This commit is contained in:
Cyrille Nofficial 2019-12-01 22:58:12 +01:00
parent 17beea7e8a
commit d990278058
2 changed files with 41 additions and 0 deletions

11
cli/cli.go Normal file
View File

@ -0,0 +1,11 @@
package cli
import "os"
func SetDefaultValueFromEnv(value *string, key string, defaultValue string) {
if os.Getenv(key) != "" {
*value = os.Getenv(key)
} else {
*value = defaultValue
}
}

30
cli/cli_test.go Normal file
View File

@ -0,0 +1,30 @@
package cli
import (
"os"
"testing"
)
func TestSetDefaultValueFromEnv(t *testing.T) {
err := os.Setenv("KEY1", "value1")
if err != nil {
t.Errorf("unable to set env value: %v", err)
}
cases := []struct{
key string
defValue string
expected string
}{
{"MISSING_KEY", "default", "default"},
{"KEY1", "bad value", "value1"},
}
for _, c := range cases {
var value = ""
SetDefaultValueFromEnv(&value, c.key, c.defValue)
if c.expected != value {
t.Errorf("SetDefaultValueFromEnv(*value, %v, %v): %v, wants %v", c.key, c.defValue, value, c.expected)
}
}
}