mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Adding automatic reading from ENV w/tests
This commit is contained in:
parent
aacc3049e2
commit
83fd92627c
2 changed files with 14 additions and 0 deletions
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 a new issue