mirror of https://github.com/spf13/viper.git
Adding automatic reading from ENV w/tests
This commit is contained in:
parent
aacc3049e2
commit
83fd92627c
8
viper.go
8
viper.go
|
@ -262,6 +262,14 @@ func IsSet(key string) bool {
|
||||||
return t != nil
|
return t != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Have viper check ENV variables for all
|
||||||
|
// keys set in config, default & flags
|
||||||
|
func AutomaticEnv() {
|
||||||
|
for _, x := range AllKeys() {
|
||||||
|
BindEnv(x)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Aliases provide another accessor for the same key.
|
// Aliases provide another accessor for the same key.
|
||||||
// This enables one to change a name without breaking the application
|
// This enables one to change a name without breaking the application
|
||||||
func RegisterAlias(alias string, key string) {
|
func RegisterAlias(alias string, key string) {
|
||||||
|
|
|
@ -135,9 +135,15 @@ func TestEnv(t *testing.T) {
|
||||||
|
|
||||||
os.Setenv("ID", "13")
|
os.Setenv("ID", "13")
|
||||||
os.Setenv("FOOD", "apple")
|
os.Setenv("FOOD", "apple")
|
||||||
|
os.Setenv("NAME", "crunk")
|
||||||
|
|
||||||
assert.Equal(t, "13", Get("id"))
|
assert.Equal(t, "13", Get("id"))
|
||||||
assert.Equal(t, "apple", Get("f"))
|
assert.Equal(t, "apple", Get("f"))
|
||||||
|
assert.Equal(t, "Cake", Get("name"))
|
||||||
|
|
||||||
|
AutomaticEnv()
|
||||||
|
|
||||||
|
assert.Equal(t, "crunk", Get("name"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAllKeys(t *testing.T) {
|
func TestAllKeys(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue