Skip to content

Commit 549bd20

Browse files
committed
feat: support days/weeks in serpent.Duration
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 659f3c4 commit 549bd20

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
github.com/pion/udp v0.1.4
1414
github.com/spf13/pflag v1.0.5
1515
github.com/stretchr/testify v1.8.4
16+
github.com/xhit/go-str2duration/v2 v2.1.0
1617
golang.org/x/crypto v0.19.0
1718
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a
1819
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
8181
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
8282
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
8383
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
84+
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
85+
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
8486
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
8587
go.opentelemetry.io/otel v1.19.0 h1:MuS/TNf4/j4IXsZuJegVzI1cwut7Qc00344rgH7p8bs=
8688
go.opentelemetry.io/otel v1.19.0/go.mod h1:i0QyjOq3UPoTzff0PJB2N66fb4S0+rSbSB15/oyH9fY=

values.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/spf13/pflag"
16+
str2duration "github.com/xhit/go-str2duration/v2"
1617
"golang.org/x/xerrors"
1718
"gopkg.in/yaml.v3"
1819
)
@@ -262,7 +263,16 @@ func DurationOf(d *time.Duration) *Duration {
262263
}
263264

264265
func (d *Duration) Set(v string) error {
265-
dd, err := time.ParseDuration(v)
266+
// Try [str2duration.ParseDuration] first, which supports days and weeks.
267+
// If it fails, fall back to [time.ParseDuration] for backward compatibility.
268+
dd, err := str2duration.ParseDuration(v)
269+
if err == nil {
270+
*d = Duration(dd)
271+
return nil
272+
}
273+
274+
// Fallback to standard [time.ParseDuration].
275+
dd, err = time.ParseDuration(v)
266276
*d = Duration(dd)
267277
return err
268278
}

0 commit comments

Comments
 (0)