mirror of
https://github.com/spf13/viper
synced 2024-12-23 03:57:01 +00:00
ensure BindPFlag() detects a nil flag parameter before wrapping in pflagValue.
This commit is contained in:
parent
d9d7dcdc63
commit
a0285163e1
2 changed files with 8 additions and 0 deletions
3
viper.go
3
viper.go
|
@ -992,6 +992,9 @@ func (v *Viper) BindPFlags(flags *pflag.FlagSet) error {
|
||||||
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
|
func BindPFlag(key string, flag *pflag.Flag) error { return v.BindPFlag(key, flag) }
|
||||||
|
|
||||||
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
|
func (v *Viper) BindPFlag(key string, flag *pflag.Flag) error {
|
||||||
|
if flag == nil {
|
||||||
|
return fmt.Errorf("flag for %q is nil", key)
|
||||||
|
}
|
||||||
return v.BindFlagValue(key, pflagValue{flag})
|
return v.BindFlagValue(key, pflagValue{flag})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -970,6 +970,11 @@ func TestBindPFlag(t *testing.T) {
|
||||||
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
assert.Equal(t, "testing_mutate", Get("testvalue"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBindPFlagDetectNilFlag(t *testing.T) {
|
||||||
|
result := BindPFlag("testvalue", nil)
|
||||||
|
assert.Error(t, result)
|
||||||
|
}
|
||||||
|
|
||||||
func TestBindPFlagStringToString(t *testing.T) {
|
func TestBindPFlagStringToString(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
Expected map[string]string
|
Expected map[string]string
|
||||||
|
|
Loading…
Reference in a new issue