You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
941 B
51 lines
941 B
package cli
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// DurationFlag - a flag of time.Duration type
|
|
type DurationFlag struct {
|
|
Name string
|
|
Value time.Duration
|
|
Usage string
|
|
EnvVar string
|
|
Hide bool
|
|
}
|
|
|
|
// String -
|
|
func (f DurationFlag) String() string {
|
|
return withEnvHint(f.EnvVar, fmt.Sprintf("%s \"%v\"\t%v", prefixedNames(f.Name), f.Value, f.Usage))
|
|
}
|
|
|
|
// Apply -
|
|
func (f DurationFlag) Apply(set *flag.FlagSet) {
|
|
if f.EnvVar != "" {
|
|
for _, envVar := range strings.Split(f.EnvVar, ",") {
|
|
envVar = strings.TrimSpace(envVar)
|
|
if envVal := os.Getenv(envVar); envVal != "" {
|
|
envValDuration, err := time.ParseDuration(envVal)
|
|
if err == nil {
|
|
f.Value = envValDuration
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
eachName(f.Name, func(name string) {
|
|
set.Duration(name, f.Value, f.Usage)
|
|
})
|
|
}
|
|
|
|
func (f DurationFlag) getName() string {
|
|
return f.Name
|
|
}
|
|
|
|
func (f DurationFlag) isNotHidden() bool {
|
|
return !f.Hide
|
|
}
|
|
|