-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Strict env prefix #1629
base: master
Are you sure you want to change the base?
feat: Strict env prefix #1629
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,6 +197,7 @@ type Viper struct { | |
configType string | ||
configPermissions os.FileMode | ||
envPrefix string | ||
envPrefixStrict bool | ||
|
||
// Specific commands for ini parsing | ||
iniLoadOptions ini.LoadOptions | ||
|
@@ -282,6 +283,15 @@ func EnvKeyReplacer(r StringReplacer) Option { | |
}) | ||
} | ||
|
||
func StrictEnvPrefix(in string) Option { | ||
return optionFunc(func(v *Viper) { | ||
if "" != in { | ||
v.envPrefix = in | ||
v.envPrefixStrict = true | ||
} | ||
}) | ||
} | ||
|
||
// NewWithOptions creates a new Viper instance. | ||
func NewWithOptions(opts ...Option) *Viper { | ||
v := New() | ||
|
@@ -519,19 +529,24 @@ func SetEnvPrefix(in string) { v.SetEnvPrefix(in) } | |
|
||
func (v *Viper) SetEnvPrefix(in string) { | ||
if in != "" { | ||
v.envPrefix = in | ||
v.envPrefix = in + "_" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why append the underscore here if there is a separate variable tracking whether it should be? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The variable tracks whether the underscore is added by viper (legacy behaviour) or by user. |
||
} | ||
} | ||
|
||
func GetEnvPrefix() string { return v.GetEnvPrefix() } | ||
|
||
func (v *Viper) GetEnvPrefix() string { | ||
return v.envPrefix | ||
p := v.envPrefix | ||
if v.envPrefixStrict && "" != p { | ||
// Removes automatic trailing underscore. | ||
p = p[:len(p)-1] | ||
} | ||
return p | ||
} | ||
|
||
func (v *Viper) mergeWithEnvPrefix(in string) string { | ||
if v.envPrefix != "" { | ||
return strings.ToUpper(v.envPrefix + "_" + in) | ||
return strings.ToUpper(v.envPrefix + in) | ||
} | ||
|
||
return strings.ToUpper(in) | ||
|
@@ -959,6 +974,7 @@ func (v *Viper) Sub(key string) *Viper { | |
subv.parents = append(v.parents, strings.ToLower(key)) | ||
subv.automaticEnvApplied = v.automaticEnvApplied | ||
subv.envPrefix = v.envPrefix | ||
subv.envPrefixStrict = v.envPrefixStrict | ||
subv.envKeyReplacer = v.envKeyReplacer | ||
subv.config = cast.ToStringMap(data) | ||
return subv | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like the name
StrictEnvPrefix
.Something like
RawEnvPrefix
orBareEnvPrefix
might sound better.An alternative I can imagine is a separate option called EnvPrefixWithDelimiter (deprecating any current options).