Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ type Viper struct {
configType string
configPermissions os.FileMode
envPrefix string
envPrefixStrict bool

// Specific commands for ini parsing
iniLoadOptions ini.LoadOptions
Expand Down Expand Up @@ -282,6 +283,15 @@ func EnvKeyReplacer(r StringReplacer) Option {
})
}

func StrictEnvPrefix(in string) Option {
Copy link
Collaborator

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 or BareEnvPrefix might sound better.

An alternative I can imagine is a separate option called EnvPrefixWithDelimiter (deprecating any current options).

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()
Expand Down Expand Up @@ -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 + "_"
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
The rest of the code does not bother whether the envPrefix was modified by viper or not.

}
}

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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,13 @@ func TestEnvPrefix(t *testing.T) {
assert.Equal(t, "crunk", Get("name"))
}

func TestEnvPrefixStrict(t *testing.T) {
v := NewWithOptions(StrictEnvPrefix("foo"))
v.BindEnv("id")
t.Setenv("FOOID", "ok")
assert.Equal(t, "ok", v.Get("id"))
}

func TestAutoEnv(t *testing.T) {
Reset()

Expand Down