implement os.ExpandEnv, and augment test to meet both forms it supports

This commit is contained in:
Nathan Trujillo 2018-11-06 14:34:04 -08:00
parent d09390e250
commit 98e3faa0ad
2 changed files with 5 additions and 4 deletions

View file

@ -222,14 +222,12 @@ func deepSearch(m map[string]interface{}, path []string) map[string]interface{}
}
// We look up a environmental variable if it looks like '${HOME}'
func lookupEnvByValue(s string) string {
match, err := regexp.MatchString("\\${[A-Za-z_-]+}", s)
match, err := regexp.MatchString("(\\$[A-Za-z_-]+|\\${[A-Za-z_-]+})", s)
if (err != nil) {
fmt.Println(match, err)
}
if (match) {
re := regexp.MustCompile("(\\$|{|}|)")
clean := re.ReplaceAllString(s, "")
env := os.Getenv(clean)
env := os.ExpandEnv(s)
if ( env != "" ) {
return env
}

View file

@ -51,6 +51,7 @@ beard: true
var yamlExampleWithEnv = []byte(`
home: '${HOME_STUFF}'
undefined: '${UNDEFINED}'
theclown: ${HOME_STUFF}
`)
var yamlExampleWithExtras = []byte(`Existing: true
@ -452,6 +453,8 @@ func TestGetStringEnv(t *testing.T) {
value := v.GetStringEnv("home")
// defined env variables are interpolated
assert.Equal(t, ".", value)
value = v.GetStringEnv("theclown")
assert.Equal(t, ".", value)
// undefined env variables are untouched
undefined_value := v.GetStringEnv("undefined")
assert.Equal(t, "${UNDEFINED}", undefined_value)