From 98e3faa0ad7ec12988c70ab2ab4926c470349f97 Mon Sep 17 00:00:00 2001 From: Nathan Trujillo Date: Tue, 6 Nov 2018 14:34:04 -0800 Subject: [PATCH] implement os.ExpandEnv, and augment test to meet both forms it supports --- util.go | 6 ++---- viper_test.go | 3 +++ 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/util.go b/util.go index 6f2d771..4c1b0da 100644 --- a/util.go +++ b/util.go @@ -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 } diff --git a/viper_test.go b/viper_test.go index b55db98..3e2e3d3 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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)