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

Add support for alias in YAMLs #1448

Merged
merged 14 commits into from Aug 30, 2022
20 changes: 12 additions & 8 deletions altsrc/flag.go
Expand Up @@ -121,14 +121,18 @@ func (f *IntSliceFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceC

// ApplyInputSourceValue applies a Bool value to the flagSet if required
func (f *BoolFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error {
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) && isc.isSet(f.BoolFlag.Name) {
value, err := isc.Bool(f.BoolFlag.Name)
if err != nil {
return err
}
if value {
for _, name := range f.Names() {
_ = f.set.Set(name, strconv.FormatBool(value))
if f.set != nil && !cCtx.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dearchap @james-prysm Do you think there is too mush nesting? Should we return early when don't need to handle. For example:

	if f.set == nil || cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars) {
		return nil
	}
	for _, name := range f.BoolFlag.Names() {
		if !isc.isSet(name) {
			continue
		}
		value, err := isc.Bool(name)
		if err != nil {
			return err
		}
		for _, n := range f.Names() {
			_ = f.set.Set(n, strconv.FormatBool(value))
		}
	}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dokiys definitely your approach is more readable. You want to make the changes and push ?

for _, name := range f.BoolFlag.Names() {
if isc.isSet(name) {
value, err := isc.Bool(name)
if err != nil {
return err
}
if value {
for _, n := range f.Names() {
_ = f.set.Set(n, strconv.FormatBool(value))
}
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions altsrc/flag_test.go
Expand Up @@ -178,6 +178,19 @@ func TestBoolApplyInputSourceMethodSet(t *testing.T) {
refute(t, true, c.Bool("test"))
}

func TestBoolApplyInputSourceMethodSet_Alias(t *testing.T) {
tis := testApplyInputSource{
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test", Aliases: []string{"test_alias"}}),
FlagName: "test_alias",
MapValue: true,
}
c := runTest(t, tis)
expect(t, true, c.Bool("test_alias"))

c = runRacyTest(t, tis)
refute(t, true, c.Bool("test_alias"))
}

func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
tis := testApplyInputSource{
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
Expand Down