Skip to content

Commit ca0942e

Browse files
committed
add go.mod; inline parseDuration
1 parent 8e9dc23 commit ca0942e

File tree

7 files changed

+96
-6
lines changed

7 files changed

+96
-6
lines changed

commandsgen/code.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"regexp"
99
"sort"
1010
"strings"
11-
12-
"go.temporal.io/server/common/primitives/timestamp"
1311
)
1412

1513
func GenerateCommandsCode(pkg string, contextType string, commands Commands) ([]byte, error) {
@@ -329,7 +327,7 @@ func (o *Option) writeFlagBuilding(selfVar, flagVar string, w *codeWriter) error
329327
case "duration":
330328
flagMeth, setDefault = "Var", "0"
331329
if o.Default != "" {
332-
dur, err := timestamp.ParseDuration(o.Default)
330+
dur, err := parseDuration(o.Default)
333331
if err != nil {
334332
return fmt.Errorf("invalid default: %w", err)
335333
}

commandsgen/go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/temporalio/cli/commandsgen
2+
3+
go 1.25.0
4+
5+
require gopkg.in/yaml.v3 v3.0.1
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
github.com/stretchr/testify v1.11.1
11+
)

commandsgen/go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
6+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
8+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
9+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
10+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

commandsgen/util.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package commandsgen
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strconv"
7+
"strings"
8+
"time"
9+
)
10+
11+
var reDays = regexp.MustCompile(`(\d+(\.\d*)?|(\.\d+))d`)
12+
13+
// parseDuration is like time.ParseDuration, but supports unit "d" for days
14+
// (always interpreted as exactly 24 hours).
15+
func parseDuration(s string) (time.Duration, error) {
16+
s = reDays.ReplaceAllStringFunc(s, func(v string) string {
17+
fv, err := strconv.ParseFloat(strings.TrimSuffix(v, "d"), 64)
18+
if err != nil {
19+
return v // will cause time.ParseDuration to return an error
20+
}
21+
return fmt.Sprintf("%fh", 24*fv)
22+
})
23+
return time.ParseDuration(s)
24+
}

commandsgen/util_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package commandsgen
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/suite"
8+
)
9+
10+
type ParseDurationSuite struct {
11+
suite.Suite
12+
}
13+
14+
func TestParseDurationSuite(t *testing.T) {
15+
suite.Run(t, new(ParseDurationSuite))
16+
}
17+
18+
func (s *ParseDurationSuite) TestParseDuration() {
19+
for _, c := range []struct {
20+
input string
21+
expected time.Duration // -1 means error
22+
}{
23+
{"1h", time.Hour},
24+
{"3m30s", 3*time.Minute + 30*time.Second},
25+
{"1d", 24 * time.Hour},
26+
{"3d", 3 * 24 * time.Hour},
27+
{"5d6h15m", 5*24*time.Hour + 6*time.Hour + 15*time.Minute},
28+
{"5.25d15m", 5*24*time.Hour + 6*time.Hour + 15*time.Minute},
29+
{".5d", 12 * time.Hour},
30+
{"-10d12.25h", -(10*24*time.Hour + 12*time.Hour + 15*time.Minute)},
31+
{"3m2h1d", 3*time.Minute + 2*time.Hour + 1*24*time.Hour},
32+
{"8m7h6d5d4h3m", 8*time.Minute + 7*time.Hour + 6*24*time.Hour + 5*24*time.Hour + 4*time.Hour + 3*time.Minute},
33+
{"7", -1}, // error
34+
{"", -1}, // error
35+
{"10000000h", -1}, // error out of bounds
36+
} {
37+
got, err := parseDuration(c.input)
38+
if c.expected == -1 {
39+
s.Error(err)
40+
} else {
41+
s.Equal(c.expected, got)
42+
}
43+
}
44+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ require (
1414
github.com/olekukonko/tablewriter v0.0.5
1515
github.com/spf13/cobra v1.9.1
1616
github.com/spf13/pflag v1.0.6
17-
github.com/stretchr/testify v1.10.0
17+
github.com/stretchr/testify v1.11.1
18+
github.com/temporalio/cli/commandsgen v0.0.0
1819
github.com/temporalio/ui-server/v2 v2.42.1
1920
go.temporal.io/api v1.53.0
2021
go.temporal.io/sdk v1.37.0
@@ -176,3 +177,5 @@ require (
176177
modernc.org/strutil v1.2.1 // indirect
177178
modernc.org/token v1.1.0 // indirect
178179
)
180+
181+
replace github.com/temporalio/cli/commandsgen => ./commandsgen

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
318318
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
319319
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
320320
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
321-
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
322-
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
321+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
322+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
323323
github.com/temporalio/ringpop-go v0.0.0-20250130211428-b97329e994f7 h1:lEebX/hZss+TSH3EBwhztnBavJVj7pWGJOH8UgKHS0w=
324324
github.com/temporalio/ringpop-go v0.0.0-20250130211428-b97329e994f7/go.mod h1:RE+CHmY+kOZQk47AQaVzwrGmxpflnLgTd6EOK0853j4=
325325
github.com/temporalio/sqlparser v0.0.0-20231115171017-f4060bcfa6cb h1:YzHH/U/dN7vMP+glybzcXRTczTrgfdRisNTzAj7La04=

0 commit comments

Comments
 (0)