Adding automatic reading from ENV w/tests
diff --git a/viper.go b/viper.go
index c45abc8..e658a8e 100644
--- a/viper.go
+++ b/viper.go
@@ -262,6 +262,14 @@
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.
// This enables one to change a name without breaking the application
func RegisterAlias(alias string, key string) {
diff --git a/viper_test.go b/viper_test.go
index fba5a18..9818138 100644
--- a/viper_test.go
+++ b/viper_test.go
@@ -135,9 +135,15 @@
os.Setenv("ID", "13")
os.Setenv("FOOD", "apple")
+ os.Setenv("NAME", "crunk")
assert.Equal(t, "13", Get("id"))
assert.Equal(t, "apple", Get("f"))
+ assert.Equal(t, "Cake", Get("name"))
+
+ AutomaticEnv()
+
+ assert.Equal(t, "crunk", Get("name"))
}
func TestAllKeys(t *testing.T) {