Support rewriting env keys
diff --git a/viper.go b/viper.go
index e808887..2d51b9b 100644
--- a/viper.go
+++ b/viper.go
@@ -77,6 +77,7 @@
envPrefix string
automaticEnvApplied bool
+ envKeyReplacer *strings.Replacer
config map[string]interface{}
override map[string]interface{}
@@ -153,6 +154,20 @@
return strings.ToUpper(in)
}
+// TODO: should getEnv logic be moved into find(). Can generalize the use of
+// rewriting keys many things, Ex: Get('someKey') -> some_key
+// (cammel case to snake case for JSON keys perhaps)
+
+// getEnv s a wrapper around os.Getenv which replaces characters in the original
+// key. This allows env vars which have different keys then the config object
+// keys
+func (v *viper) getEnv(key string) string {
+ if v.envKeyReplacer != nil {
+ key = v.envKeyReplacer.Replace(key)
+ }
+ return os.Getenv(key)
+}
+
// Return the config file used
func ConfigFileUsed() string { return v.ConfigFileUsed() }
func (v *viper) ConfigFileUsed() string { return v.configFile }
@@ -427,7 +442,7 @@
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 != "" {
+ if val = v.getEnv(v.mergeWithEnvPrefix(key)); val != "" {
jww.TRACE.Println(key, "found in environment with val:", val)
return val
}
@@ -436,7 +451,7 @@
envkey, exists := v.env[key]
if exists {
jww.TRACE.Println(key, "registered as env var", envkey)
- if val = os.Getenv(envkey); val != "" {
+ if val = v.getEnv(envkey); val != "" {
jww.TRACE.Println(envkey, "found in environment with val:", val)
return val
} else {
@@ -479,6 +494,12 @@
v.automaticEnvApplied = true
}
+// SetEnvKeyReplacer sets the strings.Replacer on the viper object
+func SetEnvKeyReplacer(r *strings.Replacer) { v.SetEnvKeyReplacer(r) }
+func (v *viper) SetEnvKeyReplacer(r *strings.Replacer) {
+ v.envKeyReplacer = r
+}
+
// 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) { v.RegisterAlias(alias, key) }