2024-08-13 14:53:26 +01:00
|
|
|
package flag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-08-20 03:32:54 +01:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-08-13 14:53:26 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-08-20 03:32:54 +01:00
|
|
|
const timeDurationRegexPattern string = `[0-9]{1,4}\s+(days?|hours?|minutes?|seconds?)`
|
|
|
|
|
2024-08-13 14:53:26 +01:00
|
|
|
type TimeDurationValue struct {
|
|
|
|
Duration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewTimeDurationValue() TimeDurationValue {
|
|
|
|
return TimeDurationValue{
|
2024-08-20 03:32:54 +01:00
|
|
|
Duration: time.Duration(0),
|
2024-08-13 14:53:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v TimeDurationValue) String() string {
|
|
|
|
return v.Duration.String()
|
|
|
|
}
|
|
|
|
|
2024-08-20 03:32:54 +01:00
|
|
|
func (v *TimeDurationValue) Set(value string) error {
|
|
|
|
pattern := regexp.MustCompile(timeDurationRegexPattern)
|
|
|
|
matches := pattern.FindAllString(value, -1)
|
|
|
|
|
|
|
|
days, hours, minutes, seconds := 0, 0, 0, 0
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
for ind := range len(matches) {
|
|
|
|
switch {
|
|
|
|
case strings.Contains(matches[ind], "day"):
|
|
|
|
days, err = parseInt(matches[ind])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse the number of days from %s: %w", matches[ind], err)
|
|
|
|
}
|
|
|
|
case strings.Contains(matches[ind], "hour"):
|
|
|
|
hours, err = parseInt(matches[ind])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse the number of hours from %s: %w", matches[ind], err)
|
|
|
|
}
|
|
|
|
case strings.Contains(matches[ind], "minute"):
|
|
|
|
minutes, err = parseInt(matches[ind])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse the number of minutes from %s: %w", matches[ind], err)
|
|
|
|
}
|
|
|
|
case strings.Contains(matches[ind], "second"):
|
|
|
|
seconds, err = parseInt(matches[ind])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to parse the number of seconds from %s: %w", matches[ind], err)
|
|
|
|
}
|
|
|
|
}
|
2024-08-13 14:53:26 +01:00
|
|
|
}
|
|
|
|
|
2024-08-20 03:32:54 +01:00
|
|
|
durationValue := (days * 86400) + (hours * 3600) + (minutes * 60) + seconds
|
|
|
|
|
|
|
|
v.Duration = time.Duration(durationValue) * time.Second
|
2024-08-13 14:53:26 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-20 03:32:54 +01:00
|
|
|
|
|
|
|
func parseInt(text string) (int, error) {
|
|
|
|
split := strings.SplitN(text, " ", 2)
|
|
|
|
if len(split) != 2 {
|
|
|
|
return 0, fmt.Errorf("unexpected number of split for %s: want 2, got %d", text, len(split))
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := strconv.Atoi(split[0])
|
|
|
|
if err != nil {
|
|
|
|
return 0, fmt.Errorf("unable to convert %s to an integer: %w", text, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|