-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
225 lines (184 loc) · 3.72 KB
/
cron.go
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
package cron
import (
"regexp"
"strconv"
"sort"
)
type section struct {
min int
max int
}
type Cron struct {
Minute []int
Hour []int
Day []int
Month []int
WeekDay []int
Command string
}
var (
// `3-6`
regexpValueRange = regexp.MustCompile("^(\\d{1,2})-(\\d{1,2})$")
// `*/5`
regexpWildcardEveryNth = regexp.MustCompile("^\\*/(\\d{1,2})$")
// `2/5`
regexpValueEveryNth = regexp.MustCompile("^(\\d{1,2})/(\\d{1,2})$")
// `2-5/4`
regexpRangeAndEveryNth = regexp.MustCompile("^(\\d{1,2})-(\\d{1,2})/(\\d+)$")
)
var (
minute = section{min: 0, max: 59}
hour = section{min: 0, max: 23}
days = section{min: 1, max: 31}
month = section{min: 1, max: 12}
weekday = section{min: 0, max: 6}
)
func Parse(args... string) *Cron {
for i := range args {
if i + 5 > len(args) {
return nil
}
m := parseMinute(args[i])
if len(m) == 0 {
continue
}
h := parseHour(args[i + 1])
if len(h) == 0 {
continue
}
d := parseDay(args[i + 2])
if len(d) == 0 {
continue
}
mo := parseMonth(args[i + 3])
if len(mo) == 0 {
continue
}
w := parseWeekday(args[i + 4])
if len(w) == 0 {
continue
}
c := &Cron{
Minute: m,
Hour: h,
Day: d,
Month: mo,
WeekDay: w,
}
if len(args) > i + 5 {
c.Command = args[i + 5]
}
return c
}
return nil
}
func parseMinute(raw string) []int {
return parseSection(minute, raw)
}
func parseHour(raw string) []int {
return parseSection(hour, raw)
}
func parseDay(raw string) []int {
return parseSection(days, raw)
}
func parseMonth(raw string) []int {
return parseSection(month, raw)
}
func parseWeekday(raw string) []int {
return parseSection(weekday, raw)
}
func parseSection(s section, raw string) []int {
r := s.parse(raw)
var el = make(map[int]struct{})
// remove duplicates
for i := range r {
if _, ok := el[r[i]]; ok {
r = append(r[:i], r[i+1:]...)
}
el[r[i]] = struct{}{}
}
// sort, although if should be already sorted :)
sort.Ints(r)
return r
}
func (s section) defaultRange() []int {
return customRange(s.min, s.max)
}
func (s section) inRange(v int) bool {
return s.min <= v && s.max >= v
}
func (s section) int(v string) int {
i, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
return i
}
func (s section) parse(raw string) []int {
if raw == "*" {
return s.defaultRange()
}
if intval, err := strconv.Atoi(raw); err == nil {
if s.inRange(intval) {
return []int{intval}
}
return []int{}
}
matches := regexpValueRange.FindStringSubmatch(raw)
if len(matches) > 0 {
min, max := s.int(matches[1]), s.int(matches[2])
if s.inRange(min) && s.inRange(max) && min < max {
return customRange(min, max)
}
return []int{}
}
matches = regexpWildcardEveryNth.FindStringSubmatch(raw)
if len(matches) > 0 {
v := s.int(matches[1])
if s.inRange(v) && v > 0 {
return rangeEveryNth(s.min, s.max, v)
}
return []int{}
}
matches = regexpValueEveryNth.FindStringSubmatch(raw)
if len(matches) > 0 {
min, max := s.int(matches[1]), s.max
if s.inRange(min) && s.inRange(max) && min < max && min > 0 {
return rangeEveryNth(min, s.max, s.int(matches[2]))
}
return []int{}
}
matches = regexpRangeAndEveryNth.FindStringSubmatch(raw)
if len(matches) > 0 {
min, max := s.int(matches[1]), s.int(matches[2])
if s.inRange(min) && s.inRange(max) && min < max {
return rangeEveryNth(min, max, s.int(matches[3]))
}
return []int{}
}
return []int{}
}
func rangeEveryNth(min, max, nth int) []int {
var (
r = make([]int, 0)
i = 1
)
for {
cur := i * nth
if cur > max {
break
}
if cur >= min {
r = append(r, i * nth)
}
i++
}
return r
}
func customRange(min, max int) []int {
var r = make([]int, 0, max - min)
for i := min; i <= max; i++ {
r = append(r, i)
}
return r
}