Adding automatic reading from ENV w/tests

This commit is contained in:
spf13 2014-09-27 14:01:11 -07:00
parent aacc3049e2
commit 83fd92627c
2 changed files with 14 additions and 0 deletions

View file

@ -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) {

View file

@ -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) {