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.
32 lines
759 B
32 lines
759 B
2 years ago
|
package variable
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
"gitoa.ru/go-4devs/console/input/flag"
|
||
|
"gitoa.ru/go-4devs/console/input/value"
|
||
|
)
|
||
|
|
||
|
func Duration(name, description string, opts ...Option) Variable {
|
||
|
return String(name, description, append(opts, WithParse(CreateDuration, AppendDuration), Value(flag.Duration))...)
|
||
|
}
|
||
|
|
||
|
func CreateDuration(in string) (value.Value, error) {
|
||
|
out, err := time.ParseDuration(in)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("create duration:%w", err)
|
||
|
}
|
||
|
|
||
|
return value.NewDuration(out), nil
|
||
|
}
|
||
|
|
||
|
func AppendDuration(old value.Value, in string) (value.Value, error) {
|
||
|
out, err := time.ParseDuration(in)
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("append duration:%w", err)
|
||
|
}
|
||
|
|
||
|
return value.NewDurations(append(old.Durations(), out)), nil
|
||
|
}
|