fs: make all duration flags take y, M, w, d etc suffixes

Fixes #6556
This commit is contained in:
Nick Craig-Wood 2022-11-10 17:28:12 +00:00
parent 2f77651f64
commit 0ac5795f8c
1 changed files with 8 additions and 4 deletions

View File

@ -146,18 +146,22 @@ func Float64VarP(flags *pflag.FlagSet, p *float64, name, shorthand string, value
// DurationP defines a flag which can be set by an environment variable
//
// It is a thin wrapper around pflag.DurationP
// It wraps the duration in an fs.Duration for extra suffixes and
// passes it to pflag.VarP
func DurationP(name, shorthand string, value time.Duration, usage string) (out *time.Duration) {
out = pflag.DurationP(name, shorthand, value, usage)
out = new(time.Duration)
*out = value
pflag.VarP((*fs.Duration)(out), name, shorthand, usage)
setValueFromEnv(pflag.CommandLine, name)
return out
}
// DurationVarP defines a flag which can be set by an environment variable
//
// It is a thin wrapper around pflag.DurationVarP
// It wraps the duration in an fs.Duration for extra suffixes and
// passes it to pflag.VarP
func DurationVarP(flags *pflag.FlagSet, p *time.Duration, name, shorthand string, value time.Duration, usage string) {
flags.DurationVarP(p, name, shorthand, value, usage)
flags.VarP((*fs.Duration)(p), name, shorthand, usage)
setValueFromEnv(flags, name)
}