Skip to content

Commit 40bdb7d

Browse files
committed
fix(databases): accepts more format
1 parent a820e2a commit 40bdb7d

File tree

2 files changed

+85
-3
lines changed

2 files changed

+85
-3
lines changed

cmd/databases.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"strconv"
78
"strings"
89
"time"
@@ -314,9 +315,9 @@ func parseScheduleAtFlag(flag string) (int, *time.Location, error) {
314315
scheduleAtTime := s[0]
315316
scheduleAtTimezone := s[1]
316317

317-
// If client writes "4:00", we only want to keep "4" in scheduleAtTime
318-
scheduleAtTimeSplit := strings.SplitN(scheduleAtTime, ":", 2)
319-
scheduleAtTime = scheduleAtTimeSplit[0]
318+
// If client writes "4:00" or "4h00", we only want to keep "4" in scheduleAtTime
319+
re := regexp.MustCompile(`([:hH].*)|[0]+`)
320+
scheduleAtTime = re.ReplaceAllString(scheduleAtTime, "")
320321

321322
scheduleAtHour, err = strconv.Atoi(scheduleAtTime)
322323
if err != nil {

cmd/databases_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestParseScheduleAtFlag(t *testing.T) {
12+
tests := map[string]struct {
13+
flag string
14+
expectedHour int
15+
expectedLoc string
16+
expectedError string
17+
}{
18+
"single hour uses local timezone": {
19+
flag: "3",
20+
expectedHour: 3,
21+
expectedLoc: time.Local.String(),
22+
},
23+
"hour and timezone": {
24+
flag: "3 Europe/Paris",
25+
expectedHour: 3,
26+
expectedLoc: "Europe/Paris",
27+
},
28+
"hour with minutes and timezone (1)": {
29+
flag: "4:00 Europe/Paris",
30+
expectedHour: 4,
31+
expectedLoc: "Europe/Paris",
32+
},
33+
"hour with minutes and timezone (2)": {
34+
flag: "4h00 Europe/Paris",
35+
expectedHour: 4,
36+
expectedLoc: "Europe/Paris",
37+
},
38+
"hour with minutes and timezone (3)": {
39+
flag: "4H00 Europe/Paris",
40+
expectedHour: 4,
41+
expectedLoc: "Europe/Paris",
42+
},
43+
"invalid flag (1)": {
44+
flag: "invalid",
45+
expectedError: "schedule-at=only contains two space-separated fields",
46+
},
47+
"invalid flag (2)": {
48+
flag: "invalid invalid2",
49+
expectedError: "schedule-at=first field must be a digit representing the hour",
50+
},
51+
"invalid flag (3)": {
52+
flag: "invalid invalid2:invalid3",
53+
expectedError: "schedule-at=first field must be a digit representing the hour",
54+
},
55+
"invalid hour": {
56+
flag: "aa Europe/Paris",
57+
expectedError: "schedule-at=first field must be a digit representing the hour",
58+
},
59+
"unknown timezone": {
60+
flag: "3 Europe/Unknown",
61+
expectedError: "schedule-at=unknown timezoneEurope/Unknown",
62+
},
63+
}
64+
65+
for name, test := range tests {
66+
t.Run(name, func(t *testing.T) {
67+
hour, loc, err := parseScheduleAtFlag(test.flag)
68+
if test.expectedError != "" {
69+
require.ErrorContains(t, err, test.expectedError)
70+
assert.Equal(t, -1, hour)
71+
assert.Nil(t, loc)
72+
return
73+
}
74+
75+
require.NoError(t, err)
76+
require.NotNil(t, loc)
77+
assert.Equal(t, test.expectedHour, hour)
78+
assert.Equal(t, test.expectedLoc, loc.String())
79+
})
80+
}
81+
}

0 commit comments

Comments
 (0)