Accept a bare integer as seconds for watchpr --interval
- add agent.ParseDurationFlag: bare integer means seconds, duration strings still parse - take --interval as a string and parse it in the command - reject unparseable and non-positive values with an error naming the flag
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// PRRef identifies a single pull request by repository and number.
|
||||
@@ -48,6 +49,25 @@ func ParsePRRef(s string) (PRRef, error) {
|
||||
return PRRef{Owner: owner, Repo: repo, Number: n}, nil
|
||||
}
|
||||
|
||||
// ParseDurationFlag parses a duration flag value, accepting either a Go
|
||||
// duration string ("30s", "1h30m") or a bare integer read as seconds ("15").
|
||||
// flag names the flag so the error says which value was rejected.
|
||||
func ParseDurationFlag(flag, value string) (time.Duration, error) {
|
||||
s := strings.TrimSpace(value)
|
||||
d, err := time.ParseDuration(s)
|
||||
if err != nil {
|
||||
n, nerr := strconv.Atoi(s)
|
||||
if nerr != nil {
|
||||
return 0, fmt.Errorf("invalid --%s value %q: want a duration such as 30s, 2m or 1h30m, or a bare number of seconds such as 15", flag, value)
|
||||
}
|
||||
d = time.Duration(n) * time.Second
|
||||
}
|
||||
if d <= 0 {
|
||||
return 0, fmt.Errorf("invalid --%s value %q: must be greater than zero", flag, value)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// ParseRepo validates and splits an "owner/repo" string.
|
||||
func ParseRepo(s string) (owner, repo string, err error) {
|
||||
s = strings.TrimSpace(s)
|
||||
|
||||
Reference in New Issue
Block a user