add support for duration flag

This commit is contained in:
James He 2022-08-25 16:53:29 -05:00 committed by Dan Buch
parent 5a5789af7a
commit c512a283ea
Signed by: meatballhat
GPG Key ID: A12F782281063434
2 changed files with 23 additions and 6 deletions

View File

@ -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

View File

@ -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"}),