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: handle other options for prefix #1719

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
33 changes: 30 additions & 3 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,8 +1240,9 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
// If only a key is provided, it will use the env key matching the key, uppercased.
// If more arguments are provided, they will represent the env variable names that
// should bind to this key and will be taken in the specified order.
// EnvPrefix will be used when set when env name is not provided.
func BindEnv(input ...string) error { return v.BindEnv(input...) }
// EnvPrefix will be used when set.
func BindEnv(input ...string) error { return v.BindEnv(input...) }
func BindEnvNoPrefix(input ...string) error { return v.BindEnvNoPrefix(input...) }

func (v *Viper) BindEnv(input ...string) error {
if len(input) == 0 {
Expand All @@ -1253,7 +1254,33 @@ func (v *Viper) BindEnv(input ...string) error {
if len(input) == 1 {
v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key))
} else {
v.env[key] = append(v.env[key], input[1:]...)
for _, otherKey := range input[1:] {
v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(otherKey))
}
}

return nil
}

// BindEnvNoPrefix binds a Viper key to a ENV variable.
// ENV variables are case sensitive.
// If only a key is provided, it will use the env key matching the key, uppercased.
// If more arguments are provided, they will represent the env variable names that
// should bind to this key and will be taken in the specified order.
// EnvPrefix will not be used even when set.
func (v *Viper) BindEnvNoPrefix(input ...string) error {
if len(input) == 0 {
return fmt.Errorf("missing key to bind to")
}

key := strings.ToLower(input[0])

if len(input) == 1 {
v.env[key] = append(v.env[key], strings.ToUpper(key))
} else {
for _, otherKey := range input[1:] {
v.env[key] = append(v.env[key], strings.ToUpper(otherKey))
}
}

return nil
Expand Down
46 changes: 44 additions & 2 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,10 @@ func TestEnvPrefix(t *testing.T) {

SetEnvPrefix("foo") // will be uppercased automatically
BindEnv("id")
BindEnv("f", "FOOD") // not using prefix
BindEnv("f", "FOOD")

t.Setenv("FOO_ID", "13")
t.Setenv("FOOD", "apple")
t.Setenv("FOO_FOOD", "apple")
t.Setenv("FOO_NAME", "crunk")

assert.Equal(t, "13", Get("id"))
Expand Down Expand Up @@ -694,6 +694,48 @@ func TestAutoEnvWithPrefix(t *testing.T) {
assert.Equal(t, "13", Get("bar"))
}

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

AutomaticEnv()
replacer := strings.NewReplacer("-", "_", ".", "_")
SetEnvKeyReplacer(replacer)
SetEnvPrefix("foo")
BindEnv([]string{"bar", "baz.id", "qux"}...)

t.Setenv("FOO_BAZ_ID", "13")

assert.Equal(t, "13", Get("bar"))
}

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

AutomaticEnv()
replacer := strings.NewReplacer("-", "_", ".", "_")
SetEnvKeyReplacer(replacer)
SetEnvPrefix("foo")
BindEnvNoPrefix([]string{"bar", "OTHER"}...)

t.Setenv("OTHER", "13")

assert.Equal(t, "13", Get("bar"))
}

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

AutomaticEnv()
replacer := strings.NewReplacer("-", "_", ".", "_")
SetEnvKeyReplacer(replacer)
SetEnvPrefix("foo")
BindEnvNoPrefix([]string{"bar"}...)

t.Setenv("BAR", "13")

assert.Equal(t, "13", Get("bar"))
}

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

Expand Down
Loading