Skip to content

Commit fe0af40

Browse files
committedDec 18, 2023
feat: handle other options for prefix
1 parent 0b0a110 commit fe0af40

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed
 

‎viper.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,8 @@ func (v *Viper) BindFlagValue(key string, flag FlagValue) error {
12411241
// If more arguments are provided, they will represent the env variable names that
12421242
// should bind to this key and will be taken in the specified order.
12431243
// EnvPrefix will be used when set when env name is not provided.
1244-
func BindEnv(input ...string) error { return v.BindEnv(input...) }
1244+
func BindEnv(input ...string) error { return v.BindEnv(input...) }
1245+
func BindEnvNoPrefix(input ...string) error { return v.BindEnvNoPrefix(input...) }
12451246

12461247
func (v *Viper) BindEnv(input ...string) error {
12471248
if len(input) == 0 {
@@ -1253,7 +1254,27 @@ func (v *Viper) BindEnv(input ...string) error {
12531254
if len(input) == 1 {
12541255
v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(key))
12551256
} else {
1256-
v.env[key] = append(v.env[key], input[1:]...)
1257+
for _, otherKey := range input[1:] {
1258+
v.env[key] = append(v.env[key], v.mergeWithEnvPrefix(otherKey))
1259+
}
1260+
}
1261+
1262+
return nil
1263+
}
1264+
1265+
func (v *Viper) BindEnvNoPrefix(input ...string) error {
1266+
if len(input) == 0 {
1267+
return fmt.Errorf("missing key to bind to")
1268+
}
1269+
1270+
key := strings.ToLower(input[0])
1271+
1272+
if len(input) == 1 {
1273+
v.env[key] = append(v.env[key], strings.ToUpper(key))
1274+
} else {
1275+
for _, otherKey := range input[1:] {
1276+
v.env[key] = append(v.env[key], strings.ToUpper(otherKey))
1277+
}
12571278
}
12581279

12591280
return nil

‎viper_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,48 @@ func TestAutoEnvWithPrefix(t *testing.T) {
694694
assert.Equal(t, "13", Get("bar"))
695695
}
696696

697+
func TestAutoEnvWithPrefixAndOthers(t *testing.T) {
698+
Reset()
699+
700+
AutomaticEnv()
701+
replacer := strings.NewReplacer("-", "_", ".", "_")
702+
SetEnvKeyReplacer(replacer)
703+
SetEnvPrefix("foo")
704+
BindEnv([]string{"bar", "baz.id", "qux"}...)
705+
706+
t.Setenv("FOO_BAZ_ID", "13")
707+
708+
assert.Equal(t, "13", Get("bar"))
709+
}
710+
711+
func TestAutoEnvWithPrefixAndOthersNoPrefixArray(t *testing.T) {
712+
Reset()
713+
714+
AutomaticEnv()
715+
replacer := strings.NewReplacer("-", "_", ".", "_")
716+
SetEnvKeyReplacer(replacer)
717+
SetEnvPrefix("foo")
718+
BindEnvNoPrefix([]string{"bar", "OTHER"}...)
719+
720+
t.Setenv("OTHER", "13")
721+
722+
assert.Equal(t, "13", Get("bar"))
723+
}
724+
725+
func TestAutoEnvWithPrefixAndOthersNoPrefixSingle(t *testing.T) {
726+
Reset()
727+
728+
AutomaticEnv()
729+
replacer := strings.NewReplacer("-", "_", ".", "_")
730+
SetEnvKeyReplacer(replacer)
731+
SetEnvPrefix("foo")
732+
BindEnvNoPrefix([]string{"bar"}...)
733+
734+
t.Setenv("BAR", "13")
735+
736+
assert.Equal(t, "13", Get("bar"))
737+
}
738+
697739
func TestSetEnvKeyReplacer(t *testing.T) {
698740
Reset()
699741

0 commit comments

Comments
 (0)