mirror of
https://github.com/spf13/viper
synced 2024-12-23 12:07:02 +00:00
implement os.ExpandEnv, and augment test to meet both forms it supports
This commit is contained in:
parent
d09390e250
commit
98e3faa0ad
2 changed files with 5 additions and 4 deletions
6
util.go
6
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}'
|
// We look up a environmental variable if it looks like '${HOME}'
|
||||||
func lookupEnvByValue(s string) string {
|
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) {
|
if (err != nil) {
|
||||||
fmt.Println(match, err)
|
fmt.Println(match, err)
|
||||||
}
|
}
|
||||||
if (match) {
|
if (match) {
|
||||||
re := regexp.MustCompile("(\\$|{|}|)")
|
env := os.ExpandEnv(s)
|
||||||
clean := re.ReplaceAllString(s, "")
|
|
||||||
env := os.Getenv(clean)
|
|
||||||
if ( env != "" ) {
|
if ( env != "" ) {
|
||||||
return env
|
return env
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,6 +51,7 @@ beard: true
|
||||||
var yamlExampleWithEnv = []byte(`
|
var yamlExampleWithEnv = []byte(`
|
||||||
home: '${HOME_STUFF}'
|
home: '${HOME_STUFF}'
|
||||||
undefined: '${UNDEFINED}'
|
undefined: '${UNDEFINED}'
|
||||||
|
theclown: ${HOME_STUFF}
|
||||||
`)
|
`)
|
||||||
|
|
||||||
var yamlExampleWithExtras = []byte(`Existing: true
|
var yamlExampleWithExtras = []byte(`Existing: true
|
||||||
|
@ -452,6 +453,8 @@ func TestGetStringEnv(t *testing.T) {
|
||||||
value := v.GetStringEnv("home")
|
value := v.GetStringEnv("home")
|
||||||
// defined env variables are interpolated
|
// defined env variables are interpolated
|
||||||
assert.Equal(t, ".", value)
|
assert.Equal(t, ".", value)
|
||||||
|
value = v.GetStringEnv("theclown")
|
||||||
|
assert.Equal(t, ".", value)
|
||||||
// undefined env variables are untouched
|
// undefined env variables are untouched
|
||||||
undefined_value := v.GetStringEnv("undefined")
|
undefined_value := v.GetStringEnv("undefined")
|
||||||
assert.Equal(t, "${UNDEFINED}", undefined_value)
|
assert.Equal(t, "${UNDEFINED}", undefined_value)
|
||||||
|
|
Loading…
Reference in a new issue