Merge pull request #1 from useafterfree/GetStringEnv

Add method `GetStringEnv` which will interpolate values which look li…
This commit is contained in:
Nathan Trujillo 2018-10-18 14:31:01 -07:00 committed by GitHub
commit fea57bb522
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 0 deletions

2
go.mod
View file

@ -6,10 +6,12 @@ require (
github.com/magiconair/properties v1.8.0 github.com/magiconair/properties v1.8.0
github.com/mitchellh/mapstructure v1.0.0 github.com/mitchellh/mapstructure v1.0.0
github.com/pelletier/go-toml v1.2.0 github.com/pelletier/go-toml v1.2.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.1.2 github.com/spf13/afero v1.1.2
github.com/spf13/cast v1.2.0 github.com/spf13/cast v1.2.0
github.com/spf13/jwalterweatherman v1.0.0 github.com/spf13/jwalterweatherman v1.0.0
github.com/spf13/pflag v1.0.2 github.com/spf13/pflag v1.0.2
github.com/stretchr/testify v1.2.2
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 // indirect golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 // indirect
golang.org/x/text v0.3.0 // indirect golang.org/x/text v0.3.0 // indirect
gopkg.in/yaml.v2 v2.2.1 gopkg.in/yaml.v2 v2.2.1

5
go.sum
View file

@ -1,3 +1,4 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
@ -9,6 +10,8 @@ github.com/mitchellh/mapstructure v1.0.0 h1:vVpGvMXJPqSDh2VYHF7gsfQj8Ncx+Xw5Y1KH
github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.0.0/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg= github.com/spf13/cast v1.2.0 h1:HHl1DSRbEQN2i8tJmtS6ViPyHx35+p51amrdsiTCrkg=
@ -17,6 +20,8 @@ github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992 h1:BH3eQWeGbwRU2+wxxuuPOdFBmaiBH81O8BugSjHeTFg=
golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180906133057-8cf3aee42992/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=

17
util.go
View file

@ -17,6 +17,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"unicode" "unicode"
"regexp"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/spf13/cast" "github.com/spf13/cast"
@ -219,3 +220,19 @@ func deepSearch(m map[string]interface{}, path []string) map[string]interface{}
} }
return m return m
} }
// We look up a environmental variable if it looks like '${HOME}'
func lookupEnvByValue(s string) string {
match, err := regexp.MatchString("\\${[A-Za-z_-]+}", s)
if (err != nil) {
fmt.Println(match, err)
}
if (match) {
re := regexp.MustCompile("(\\$|{|}|)")
clean := re.ReplaceAllString(s, "")
env := os.Getenv(clean)
if ( env != "" ) {
return env
}
}
return s
}

View file

@ -715,6 +715,12 @@ func (v *Viper) GetString(key string) string {
return cast.ToString(v.Get(key)) return cast.ToString(v.Get(key))
} }
// GetStringEnv returns the value of the environmental variable associated with the key as a string.
func GetStringEnv(key string) string { return lookupEnvByValue(v.GetString(key)) }
func (v *Viper) GetStringEnv(key string) string {
return lookupEnvByValue(cast.ToString(v.Get(key)))
}
// GetBool returns the value associated with the key as a boolean. // GetBool returns the value associated with the key as a boolean.
func GetBool(key string) bool { return v.GetBool(key) } func GetBool(key string) bool { return v.GetBool(key) }
func (v *Viper) GetBool(key string) bool { func (v *Viper) GetBool(key string) bool {

View file

@ -48,6 +48,11 @@ eyes : brown
beard: true beard: true
`) `)
var yamlExampleWithEnv = []byte(`
home: '${HOME_STUFF}'
undefined: '${UNDEFINED}'
`)
var yamlExampleWithExtras = []byte(`Existing: true var yamlExampleWithExtras = []byte(`Existing: true
Bogus: true Bogus: true
`) `)
@ -437,6 +442,21 @@ func TestSetEnvKeyReplacer(t *testing.T) {
assert.Equal(t, "30s", Get("refresh-interval")) assert.Equal(t, "30s", Get("refresh-interval"))
} }
func TestGetStringEnv(t *testing.T) {
Reset()
home := "."
os.Setenv("HOME_STUFF", home)
v := New()
v.SetConfigType("yaml")
v.ReadConfig(bytes.NewBuffer(yamlExampleWithEnv))
value := v.GetStringEnv("home")
// defined env variables are interpolated
assert.Equal(t, ".", value)
// undefined env variables are untouched
undefined_value := v.GetStringEnv("undefined")
assert.Equal(t, "${UNDEFINED}", undefined_value)
}
func TestAllKeys(t *testing.T) { func TestAllKeys(t *testing.T) {
initConfigs() initConfigs()