Skip to content

Commit

Permalink
Merge pull request #1645 from dearchap/issue_1142
Browse files Browse the repository at this point in the history
Fix:(issue_1142). Allow comma in altsrc flag names
  • Loading branch information
dearchap committed Jan 15, 2023
2 parents 95b3f52 + 6d3c231 commit a150245
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 48 deletions.
105 changes: 65 additions & 40 deletions altsrc/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"strings"
"syscall"
"time"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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))
})
}
}
Expand All @@ -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))
})
}
}
Expand All @@ -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)
})
}
}
Expand All @@ -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))
})
}
}
Expand All @@ -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())
})
}
}
Expand All @@ -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)
})
}
}
Expand Down
24 changes: 16 additions & 8 deletions altsrc/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit a150245

Please sign in to comment.