From 6d3c2311b10c697085cb6c64874db03c3eacff66 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Sat, 14 Jan 2023 16:47:04 -0500 Subject: [PATCH] Fix:(issue_1142). Allow comma in altsrc flag names --- altsrc/flag.go | 105 +++++++++++++++++++++++++++----------------- altsrc/flag_test.go | 24 ++++++---- 2 files changed, 81 insertions(+), 48 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index afb4ad44cc..6fb275f09a 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -5,6 +5,7 @@ import ( "strconv" "strings" "syscall" + "time" "github.com/urfave/cli" ) @@ -85,14 +86,17 @@ func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourc func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) { - value, err := isc.StringSlice(f.StringSliceFlag.Name) - if err != nil { - return err - } + var value []string + eachName(f.StringSliceFlag.Name, func(name string) { + val, err := isc.StringSlice(name) + if err == nil && value == nil { + value = val + } + }) if value != nil { var sliceValue cli.StringSlice = value eachName(f.Name, func(name string) { - underlyingFlag := f.set.Lookup(f.Name) + underlyingFlag := f.set.Lookup(name) if underlyingFlag != nil { underlyingFlag.Value = &sliceValue } @@ -107,14 +111,17 @@ func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputS func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) { - value, err := isc.IntSlice(f.IntSliceFlag.Name) - if err != nil { - return err - } + var value []int + eachName(f.IntSliceFlag.Name, func(name string) { + val, err := isc.IntSlice(name) + if err == nil && value == nil { + value = val + } + }) if value != nil { var sliceValue cli.IntSlice = value eachName(f.Name, func(name string) { - underlyingFlag := f.set.Lookup(f.Name) + underlyingFlag := f.set.Lookup(name) if underlyingFlag != nil { underlyingFlag.Value = &sliceValue } @@ -129,13 +136,16 @@ func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) { - value, err := isc.Bool(f.BoolFlag.Name) - if err != nil { - return err - } + var value bool + eachName(f.BoolFlag.Name, func(name string) { + val, err := isc.Bool(name) + if err == nil && !value { + value = val + } + }) if value { eachName(f.Name, func(name string) { - _ = f.set.Set(f.Name, strconv.FormatBool(value)) + _ = f.set.Set(name, strconv.FormatBool(value)) }) } } @@ -147,13 +157,16 @@ func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCo func (f *BoolTFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) { - value, err := isc.BoolT(f.BoolTFlag.Name) - if err != nil { - return err - } + var value bool + eachName(f.BoolTFlag.Name, func(name string) { + val, err := isc.Bool(name) + if err == nil && !value { + value = val + } + }) if !value { eachName(f.Name, func(name string) { - _ = f.set.Set(f.Name, strconv.FormatBool(value)) + _ = f.set.Set(name, strconv.FormatBool(value)) }) } } @@ -165,13 +178,16 @@ func (f *BoolTFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceC func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) { - value, err := isc.String(f.StringFlag.Name) - if err != nil { - return err - } + var value string + eachName(f.StringFlag.Name, func(name string) { + val, err := isc.String(name) + if err == nil && value == "" { + value = val + } + }) if value != "" { eachName(f.Name, func(name string) { - _ = f.set.Set(f.Name, value) + _ = f.set.Set(name, value) }) } } @@ -183,13 +199,16 @@ func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSource func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) { - value, err := isc.Int(f.IntFlag.Name) - if err != nil { - return err - } + var value int + eachName(f.IntFlag.Name, func(name string) { + val, err := isc.Int(name) + if err == nil && value == 0 { + value = val + } + }) if value > 0 { eachName(f.Name, func(name string) { - _ = f.set.Set(f.Name, strconv.FormatInt(int64(value), 10)) + _ = f.set.Set(name, strconv.FormatInt(int64(value), 10)) }) } } @@ -201,13 +220,16 @@ func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCon func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) { - value, err := isc.Duration(f.DurationFlag.Name) - if err != nil { - return err - } + var value time.Duration + eachName(f.DurationFlag.Name, func(name string) { + val, err := isc.Duration(name) + if err == nil && value == 0 { + value = val + } + }) if value > 0 { eachName(f.Name, func(name string) { - _ = f.set.Set(f.Name, value.String()) + _ = f.set.Set(name, value.String()) }) } } @@ -219,14 +241,17 @@ func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error { if f.set != nil { if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) { - value, err := isc.Float64(f.Float64Flag.Name) - if err != nil { - return err - } + var value float64 + eachName(f.Float64Flag.Name, func(name string) { + val, err := isc.Float64(name) + if err == nil && value == 0 { + value = val + } + }) if value > 0 { floatStr := float64ToString(value) eachName(f.Name, func(name string) { - _ = f.set.Set(f.Name, floatStr) + _ = f.set.Set(name, floatStr) }) } } diff --git a/altsrc/flag_test.go b/altsrc/flag_test.go index 90a96d349d..bd77898a36 100644 --- a/altsrc/flag_test.go +++ b/altsrc/flag_test.go @@ -57,11 +57,12 @@ func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestStringSliceApplyInputSourceValue(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}), + Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test,t"}), FlagName: "test", MapValue: []interface{}{"hello", "world"}, }) expect(t, c.StringSlice("test"), []string{"hello", "world"}) + expect(t, c.StringSlice("t"), []string{"hello", "world"}) } func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) { @@ -87,11 +88,12 @@ func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestIntSliceApplyInputSourceValue(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}), + Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test,t"}), FlagName: "test", MapValue: []interface{}{1, 2}, }) expect(t, c.IntSlice("test"), []int{1, 2}) + expect(t, c.IntSlice("t"), []int{1, 2}) } func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) { @@ -117,11 +119,12 @@ func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestBoolApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}), + Flag: NewBoolFlag(cli.BoolFlag{Name: "test,t"}), FlagName: "test", MapValue: true, }) expect(t, true, c.Bool("test")) + expect(t, true, c.Bool("t")) } func TestBoolApplyInputSourceMethodContextSet(t *testing.T) { @@ -147,11 +150,12 @@ func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestBoolTApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}), + Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test, t"}), FlagName: "test", MapValue: false, }) expect(t, false, c.BoolT("test")) + expect(t, false, c.BoolT("t")) } func TestBoolTApplyInputSourceMethodContextSet(t *testing.T) { @@ -177,11 +181,12 @@ func TestBoolTApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestStringApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewStringFlag(cli.StringFlag{Name: "test"}), + Flag: NewStringFlag(cli.StringFlag{Name: "test,t"}), FlagName: "test", MapValue: "hello", }) expect(t, "hello", c.String("test")) + expect(t, "hello", c.String("t")) } func TestStringApplyInputSourceMethodContextSet(t *testing.T) { @@ -207,11 +212,12 @@ func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestIntApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewIntFlag(cli.IntFlag{Name: "test"}), + Flag: NewIntFlag(cli.IntFlag{Name: "test,t"}), FlagName: "test", MapValue: 15, }) expect(t, 15, c.Int("test")) + expect(t, 15, c.Int("t")) } func TestIntApplyInputSourceMethodContextSet(t *testing.T) { @@ -237,11 +243,12 @@ func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestDurationApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}), + Flag: NewDurationFlag(cli.DurationFlag{Name: "test,t"}), FlagName: "test", MapValue: 30 * time.Second, }) expect(t, 30*time.Second, c.Duration("test")) + expect(t, 30*time.Second, c.Duration("t")) } func TestDurationApplyInputSourceMethodContextSet(t *testing.T) { @@ -267,11 +274,12 @@ func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) { func TestFloat64ApplyInputSourceMethodSet(t *testing.T) { c := runTest(t, testApplyInputSource{ - Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}), + Flag: NewFloat64Flag(cli.Float64Flag{Name: "test,t"}), FlagName: "test", MapValue: 1.3, }) expect(t, 1.3, c.Float64("test")) + expect(t, 1.3, c.Float64("t")) } func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {