-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspec.go
More file actions
122 lines (110 loc) · 3.32 KB
/
spec.go
File metadata and controls
122 lines (110 loc) · 3.32 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
package kronasje
import (
"regexp"
"fmt"
)
// This spec tries to adhere to the 4th Berkely Distribution of the crontab
// manual (man 5 crontab) dated 19 April 2010.
// Regular expression strings
const (
startExp = `^`
endExp = `$`
everyExp = `\*`
singleOrDoubleDigitExp = `([\d]{1,2})`
aliasExp = `([[:alpha:]]{3})`
stepExp = `/` + singleOrDoubleDigitExp
numberRangeExp = singleOrDoubleDigitExp + `-` + singleOrDoubleDigitExp
listExp = singleOrDoubleDigitExp + `(?:,\s*` + singleOrDoubleDigitExp + `)*`
nameExp = `@[[:alpha:]]+`
)
var (
every = regexp.MustCompile(startExp + everyExp + endExp)
step = regexp.MustCompile(startExp + stepExp + endExp)
everyStep = regexp.MustCompile(startExp + everyExp + stepExp + endExp)
singleOrDoubleDigit = regexp.MustCompile(startExp + singleOrDoubleDigitExp + endExp)
alias = regexp.MustCompile(startExp + aliasExp + endExp)
numberRange = regexp.MustCompile(startExp + numberRangeExp + endExp)
list = regexp.MustCompile(startExp + listExp + endExp)
rangeStep = regexp.MustCompile(startExp + numberRangeExp + stepExp + endExp)
name = regexp.MustCompile(startExp + nameExp + endExp)
)
// Days and months can be specified with named aliases such as "mon", "jan", etc.
type aliases map[string]uint8
// Every field has a minimum and maximum value and possibly aliases.
type fieldSpec struct {
Min uint8
Max uint8
Aliases aliases
}
// Dealias returns the value aliased value by the given alias. Error returned if the field has no such alias or no aliases.
func (f *fieldSpec) Dealias(alias string) (uint8, error) {
if f.Aliases == nil {
return 0, fmt.Errorf("field has no aliases")
}
if number, ok := f.Aliases[alias]; !ok {
return 0, fmt.Errorf(`"%v" is not a valid alias`, alias)
} else {
return number, nil
}
}
// InRange returns a boolean indicating if the given number lies in the range of the minimum and maximum value of the field spec.
func (f *fieldSpec) InRange(number uint8) bool {
if number < f.Min || number > f.Max {
return false
} else {
return true
}
}
func (f *fieldSpec) String() string {
return fmt.Sprintf("min %v, max %v, aliases %+v", f.Min, f.Max, f.Aliases)
}
type fields struct {
minute *fieldSpec
hour *fieldSpec
dom *fieldSpec
month *fieldSpec
dow *fieldSpec
}
var spec = &fields{
minute: &fieldSpec{0, 59, nil},
hour: &fieldSpec{0, 23, nil},
dom: &fieldSpec{1, 31, nil},
month: &fieldSpec{1, 12,
aliases{
"jan": 1,
"feb": 2,
"mar": 3,
"apr": 4,
"may": 5,
"jun": 6,
"jul": 7,
"aug": 8,
"sep": 9,
"okt": 10,
"nov": 11,
"des": 12,
},
},
dow: &fieldSpec{0, 7,
aliases{
"sun": 0,
"mon": 1,
"tue": 2,
"wed": 3,
"thu": 4,
"fri": 5,
"sat": 6,
// "sun": 7,
},
},
}
// Common cron expressions can be specified using names
var names = map[string]string{
"@yearly": "0 0 1 1 *", // 1st day in the 1st month at midnight
"@annually": "@yearly",
"@monthly": "0 0 1 * *", // 1st day of every month at midnight
"@weekly": "0 0 * * 0", // Every sunday at midnight
"@daily": "0 0 * * *", // Every day at noon
"@midnight": "@daily",
"@hourly": "0 * * * *", // Every hour
}