More intelligent AutomaticEnv behavior and updated documentation.
diff --git a/viper.go b/viper.go
index d075a95..89834b7 100644
--- a/viper.go
+++ b/viper.go
@@ -76,6 +76,8 @@
configType string
envPrefix string
+ automaticEnvApplied bool
+
config map[string]interface{}
override map[string]interface{}
defaults map[string]interface{}
@@ -136,10 +138,10 @@
func (v *viper) mergeWithEnvPrefix(in string) string {
if v.envPrefix != "" {
- return v.envPrefix + "_" + in
+ return strings.ToUpper(v.envPrefix + "_" + in)
}
- return in
+ return strings.ToUpper(in)
}
// Return the config file used
@@ -370,7 +372,7 @@
key = strings.ToLower(input[0])
if len(input) == 1 {
- envkey = strings.ToUpper(v.mergeWithEnvPrefix(key))
+ envkey = v.mergeWithEnvPrefix(key)
} else {
envkey = input[1]
}
@@ -406,6 +408,15 @@
return val
}
+ if v.automaticEnvApplied {
+ // even if it hasn't been registered, if automaticEnv is used,
+ // check any Get request
+ if val = os.Getenv(v.mergeWithEnvPrefix(key)); val != "" {
+ jww.TRACE.Println(key, "found in environment with val:", val)
+ return val
+ }
+ }
+
envkey, exists := v.env[key]
if exists {
jww.TRACE.Println(key, "registered as env var", envkey)
@@ -449,9 +460,7 @@
// keys set in config, default & flags
func AutomaticEnv() { v.AutomaticEnv() }
func (v *viper) AutomaticEnv() {
- for _, x := range v.AllKeys() {
- v.BindEnv(x)
- }
+ v.automaticEnvApplied = true
}
// Aliases provide another accessor for the same key.