From c512a283ea54f68663ea1ddf1d4d75c599099b17 Mon Sep 17 00:00:00 2001 From: James He Date: Thu, 25 Aug 2022 16:53:29 -0500 Subject: [PATCH] add support for duration flag --- altsrc/flag.go | 16 ++++++++++------ altsrc/flag_test.go | 13 +++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/altsrc/flag.go b/altsrc/flag.go index a57ed99..48b394d 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -204,12 +204,16 @@ func (f *IntFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContex // ApplyInputSourceValue applies a Duration value to the flagSet if required func (f *DurationFlag) ApplyInputSourceValue(cCtx *cli.Context, isc InputSourceContext) error { if f.set != nil && !(cCtx.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) && isc.isSet(f.DurationFlag.Name) { - value, err := isc.Duration(f.DurationFlag.Name) - if err != nil { - return err - } - for _, name := range f.Names() { - _ = f.set.Set(name, value.String()) + for _, name := range f.DurationFlag.Names() { + if isc.isSet(name) { + value, err := isc.Duration(name) + if err != nil { + return err + } + for _, n := range f.Names() { + _ = f.set.Set(n, value.String()) + } + } } } return nil diff --git a/altsrc/flag_test.go b/altsrc/flag_test.go index 6b92d7f..43f26bd 100644 --- a/altsrc/flag_test.go +++ b/altsrc/flag_test.go @@ -400,6 +400,19 @@ func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) { refute(t, 12, c.Int("test")) } +func TestDurationApplyInputSourceMethodSet_Alias(t *testing.T) { + tis := testApplyInputSource{ + Flag: NewDurationFlag(&cli.DurationFlag{Name: "test", Aliases: []string{"test_alias"}}), + FlagName: "test", + MapValue: 30 * time.Second, + } + c := runTest(t, tis) + expect(t, 30*time.Second, c.Duration("test_alias")) + + c = runRacyTest(t, tis) + refute(t, 30*time.Second, c.Duration("test_alias")) +} + func TestDurationApplyInputSourceMethodSet(t *testing.T) { tis := testApplyInputSource{ Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),