-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathutil.go
More file actions
294 lines (265 loc) · 7.63 KB
/
util.go
File metadata and controls
294 lines (265 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package cli
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/schedule/cron"
"github.com/coder/coder/v2/coderd/util/tz"
"github.com/coder/serpent"
)
var (
errInvalidScheduleFormat = xerrors.New("Schedule must be in the format Mon-Fri 09:00AM America/Chicago")
errInvalidTimeFormat = xerrors.New("Start time must be in the format hh:mm[am|pm] or HH:MM")
errUnsupportedTimezone = xerrors.New("The location you provided looks like a timezone. Check https://ipinfo.io for your location.")
)
// userSetOption returns true if the option was set by the user.
// This is helpful if the zero value of a flag is meaningful, and you need
// to distinguish between the user setting the flag to the zero value and
// the user not setting the flag at all.
func userSetOption(inv *serpent.Invocation, flagName string) bool {
for _, opt := range inv.Command.Options {
if opt.Name == flagName {
return !(opt.ValueSource == serpent.ValueSourceNone || opt.ValueSource == serpent.ValueSourceDefault)
}
}
return false
}
// durationDisplay formats a duration for easier display:
// - Durations of 24 hours or greater are displays as Xd
// - Durations less than 1 minute are displayed as <1m
// - Duration is truncated to the nearest minute
// - Empty minutes and seconds are truncated
// - The returned string is the absolute value. Use sign()
// if you need to indicate if the duration is positive or
// negative.
func durationDisplay(d time.Duration) string {
duration := d
sign := ""
if duration == 0 {
return "0s"
}
if duration < 0 {
duration *= -1
}
// duration > 0 now
if duration < time.Minute {
return sign + "<1m"
}
if duration > 24*time.Hour {
duration = duration.Truncate(time.Hour)
}
if duration > time.Minute {
duration = duration.Truncate(time.Minute)
}
days := 0
for duration.Hours() >= 24 {
days++
duration -= 24 * time.Hour
}
durationDisplay := duration.String()
if days > 0 {
durationDisplay = fmt.Sprintf("%dd%s", days, durationDisplay)
}
for _, suffix := range []string{"m0s", "h0m", "d0s", "d0h"} {
if strings.HasSuffix(durationDisplay, suffix) {
durationDisplay = durationDisplay[:len(durationDisplay)-2]
}
}
return sign + durationDisplay
}
// timeDisplay formats a time in the local timezone
// in RFC3339 format.
func timeDisplay(t time.Time) string {
localTz, err := tz.TimezoneIANA()
if err != nil {
localTz = time.UTC
}
return t.In(localTz).Format(time.RFC3339)
}
// relative relativizes a duration with the prefix "ago" or "in"
func relative(d time.Duration) string {
if d > 0 {
return "in " + durationDisplay(d)
}
if d < 0 {
return durationDisplay(d) + " ago"
}
return "now"
}
// parseCLISchedule parses a schedule in the format HH:MM{AM|PM} [DOW] [LOCATION]
func parseCLISchedule(parts ...string) (*cron.Schedule, error) {
// If the user was careful and quoted the schedule, un-quote it.
// In the case that only time was specified, this will be a no-op.
if len(parts) == 1 {
parts = strings.Fields(parts[0])
}
var loc *time.Location
dayOfWeek := "*"
t, err := parseTime(parts[0])
if err != nil {
return nil, err
}
hour, minute := t.Hour(), t.Minute()
// Any additional parts get ignored.
switch len(parts) {
case 3:
dayOfWeek = parts[1]
loc, err = time.LoadLocation(parts[2])
if err != nil {
_, err = time.Parse("MST", parts[2])
if err == nil {
return nil, errUnsupportedTimezone
}
return nil, xerrors.Errorf("Invalid timezone %q specified: a valid IANA timezone is required", parts[2])
}
case 2:
// Did they provide day-of-week or location?
if maybeLoc, err := time.LoadLocation(parts[1]); err != nil {
// Assume day-of-week.
dayOfWeek = parts[1]
} else {
loc = maybeLoc
}
case 1: // already handled
default:
return nil, errInvalidScheduleFormat
}
// If location was not specified, attempt to automatically determine it as a last resort.
if loc == nil {
loc, err = tz.TimezoneIANA()
if err != nil {
loc = time.UTC
}
}
sched, err := cron.Weekly(fmt.Sprintf(
"CRON_TZ=%s %d %d * * %s",
loc.String(),
minute,
hour,
dayOfWeek,
))
if err != nil {
// This will either be an invalid dayOfWeek or an invalid timezone.
return nil, xerrors.Errorf("Invalid schedule: %w", err)
}
return sched, nil
}
// parseDuration parses a duration from a string.
// If units are omitted, minutes are assumed.
func parseDuration(raw string) (time.Duration, error) {
// If the user input a raw number, assume minutes
if isDigit(raw) {
raw += "m"
}
d, err := time.ParseDuration(raw)
if err != nil {
return 0, err
}
return d, nil
}
func isDigit(s string) bool {
return strings.IndexFunc(s, func(c rune) bool {
return c < '0' || c > '9'
}) == -1
}
// extendedParseDuration is a more lenient version of parseDuration that allows
// for more flexible input formats and cumulative durations.
// It allows for some extra units:
// - d (days, interpreted as 24h)
// - y (years, interpreted as 8_760h)
//
// FIXME: handle fractional values as discussed in https://github.com/coder/coder/pull/15040#discussion_r1799261736
func extendedParseDuration(raw string) (time.Duration, error) {
var d int64
isPositive := true
// handle negative durations by checking for a leading '-'
if strings.HasPrefix(raw, "-") {
raw = raw[1:]
isPositive = false
}
if raw == "" {
return 0, xerrors.Errorf("invalid duration: %q", raw)
}
// Regular expression to match any characters that do not match the expected duration format
invalidCharRe := regexp.MustCompile(`[^0-9|nsuµhdym]+`)
if invalidCharRe.MatchString(raw) {
return 0, xerrors.Errorf("invalid duration format: %q", raw)
}
// Regular expression to match numbers followed by 'd', 'y', or time units
re := regexp.MustCompile(`(-?\d+)(ns|us|µs|ms|s|m|h|d|y)`)
matches := re.FindAllStringSubmatch(raw, -1)
for _, match := range matches {
var num int64
num, err := strconv.ParseInt(match[1], 10, 0)
if err != nil {
return 0, xerrors.Errorf("invalid duration: %q", match[1])
}
switch match[2] {
case "d":
// we want to check if d + num * int64(24*time.Hour) would overflow
if d > (1<<63-1)-num*int64(24*time.Hour) {
return 0, xerrors.Errorf("invalid duration: %q", raw)
}
d += num * int64(24*time.Hour)
case "y":
// we want to check if d + num * int64(8760*time.Hour) would overflow
if d > (1<<63-1)-num*int64(8760*time.Hour) {
return 0, xerrors.Errorf("invalid duration: %q", raw)
}
d += num * int64(8760*time.Hour)
case "h", "m", "s", "ns", "us", "µs", "ms":
partDuration, err := time.ParseDuration(match[0])
if err != nil {
return 0, xerrors.Errorf("invalid duration: %q", match[0])
}
if d > (1<<63-1)-int64(partDuration) {
return 0, xerrors.Errorf("invalid duration: %q", raw)
}
d += int64(partDuration)
default:
return 0, xerrors.Errorf("invalid duration unit: %q", match[2])
}
}
if !isPositive {
return -time.Duration(d), nil
}
return time.Duration(d), nil
}
// parseTime attempts to parse a time (no date) from the given string using a number of layouts.
func parseTime(s string) (time.Time, error) {
// Try a number of possible layouts.
for _, layout := range []string{
time.Kitchen, // 03:04PM
"03:04pm",
"3:04PM",
"3:04pm",
"15:04",
"1504",
"03PM",
"03pm",
"3PM",
"3pm",
} {
t, err := time.Parse(layout, s)
if err == nil {
return t, nil
}
}
return time.Time{}, errInvalidTimeFormat
}
func formatActiveDevelopers(n int) string {
developerText := "developer"
if n != 1 {
developerText = "developers"
}
var nStr string
if n < 0 {
nStr = "-"
} else {
nStr = strconv.Itoa(n)
}
return fmt.Sprintf("%s active %s", nStr, developerText)
}