-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from projectdiscovery/issue-119-improve-durat…
…ion-var support days unit with DuarationVar
- Loading branch information
Showing
6 changed files
with
112 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package goflags | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
timeutil "github.com/projectdiscovery/utils/time" | ||
) | ||
|
||
type durationValue time.Duration | ||
|
||
func newDurationValue(val time.Duration, p *time.Duration) *durationValue { | ||
*p = val | ||
return (*durationValue)(p) | ||
} | ||
|
||
func (d *durationValue) Set(s string) error { | ||
v, err := timeutil.ParseDuration(s) | ||
if err != nil { | ||
err = errors.New("parse error") | ||
} | ||
*d = durationValue(v) | ||
return err | ||
} | ||
|
||
func (d *durationValue) Get() any { return time.Duration(*d) } | ||
|
||
func (d *durationValue) String() string { return (*time.Duration)(d).String() } | ||
|
||
// DurationVar adds a duration flag with a longname | ||
func (flagSet *FlagSet) DurationVar(field *time.Duration, long string, defaultValue time.Duration, usage string) *FlagData { | ||
return flagSet.DurationVarP(field, long, "", defaultValue, usage) | ||
} | ||
|
||
// DurationVarP adds a duration flag with a short name and long name. | ||
// It is equivalent to DurationVar but also allows specifying durations in days (e.g., "2d" for 2 days, which is equivalent to 2*24h). | ||
// The default unit for durations is seconds (ex: "10" => 10s). | ||
func (flagSet *FlagSet) DurationVarP(field *time.Duration, long, short string, defaultValue time.Duration, usage string) *FlagData { | ||
flagData := &FlagData{ | ||
usage: usage, | ||
long: long, | ||
defaultValue: defaultValue, | ||
} | ||
if short != "" { | ||
flagData.short = short | ||
flagSet.CommandLine.Var(newDurationValue(defaultValue, field), short, usage) | ||
flagSet.flagKeys.Set(short, flagData) | ||
} | ||
flagSet.CommandLine.Var(newDurationValue(defaultValue, field), long, usage) | ||
flagSet.flagKeys.Set(long, flagData) | ||
return flagData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package goflags | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDurationVar(t *testing.T) { | ||
t.Run("day-unit", func(t *testing.T) { | ||
var tt time.Duration | ||
flagSet := NewFlagSet() | ||
flagSet.CreateGroup("Config", "Config", | ||
flagSet.DurationVarP(&tt, "time-out", "tm", 0, "timeout for the process"), | ||
) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"-time-out", "2d", | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, 2*24*time.Hour, tt) | ||
tearDown(t.Name()) | ||
}) | ||
|
||
t.Run("without-unit", func(t *testing.T) { | ||
var tt time.Duration | ||
flagSet := NewFlagSet() | ||
flagSet.CreateGroup("Config", "Config", | ||
flagSet.DurationVarP(&tt, "time-out", "tm", 0, "timeout for the process"), | ||
) | ||
os.Args = []string{ | ||
os.Args[0], | ||
"-time-out", "2", | ||
} | ||
err := flagSet.Parse() | ||
assert.Nil(t, err) | ||
assert.Equal(t, 2*time.Second, tt) | ||
tearDown(t.Name()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters