-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
161 lines (149 loc) · 3.65 KB
/
config.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
package cronfab
import (
"errors"
"os"
"strconv"
"strings"
"time"
)
var (
ErrMaxit = errors.New("maximum number of iterations met")
)
// CrontabConfig models the possible time specifications for a crontab entry
type CrontabConfig struct {
Fields []FieldConfig
FieldUnits map[string][]int
Units []Unit
}
// NewCrontabConfig return a new crontab config for the supplied filed configs
func NewCrontabConfig(fields []FieldConfig) *CrontabConfig {
q := &CrontabConfig{
Fields: fields,
FieldUnits: map[string][]int{},
}
unms := map[string]struct{}{}
for i := 0; i < len(q.Fields); i++ {
f := fields[i]
u := f.unit
unm := u.String()
q.FieldUnits[unm] = append(q.FieldUnits[unm], i)
_, ok := unms[unm]
if !ok {
unms[unm] = struct{}{}
q.Units = append(q.Units, u)
}
}
SortUnits(q.Units)
// fields are sorted in unit order
return q
}
var (
MaxIt = 20000
)
func init() {
x, _ := strconv.Atoi(os.Getenv("CRONFAB_MAXIT"))
if x != 0 {
MaxIt = x
}
}
// Next return the next time after n as specified in the CrontabLine
func (cc *CrontabConfig) Next(ctl CrontabLine, n time.Time) (time.Time, error) {
unitsRank := cc.Units
u := unitsRank[0]
n = u.Add(n, 1)
roll := false
newn := time.Time{}
k := 0
j := 0
for k < len(unitsRank) {
for k = 0; k < len(unitsRank); k++ {
u = unitsRank[k]
fieldsForUnit := cc.FieldUnits[u.String()]
for _, i := range fieldsForUnit {
newn, roll = cc.Fields[i].Ceil(ctl[i], n)
if !newn.Equal(n) || roll {
break
}
}
if roll {
newn = unitsRank[k].Add(newn, 1)
newn = unitsRank[k].Trunc(newn)
}
if !newn.Equal(n) || roll {
break
}
}
if j > MaxIt {
return n, ErrMaxit
}
j++
n = newn
}
return n, nil
}
// NameToNumber convert a constraint mnemonic to an index
func (cc *CrontabConfig) NameToNumber(i int, s string) int {
return lookupNameIndex(cc.Fields[i].rangeNames, s)
}
// SetGroupName convert the group member name to an index for field fieldi
func (cc *CrontabConfig) SetGroupName(state State, i, fieldi int, a *CrontabConstraint, s string) error {
k := cc.NameToNumber(fieldi, s) + cc.Fields[fieldi].min
if k < cc.Fields[fieldi].min || k > cc.Fields[fieldi].max {
return &ErrorBadIndex{FieldName: cc.Fields[fieldi].name, Value: k}
}
if state == StateInName {
*a = [3]int{k, k, 1}
} else if state == StateInEndRangeName {
(*a)[1] = k
} else {
return &ErrorParse{Index: i, State: state}
}
return nil
}
// SetGroupNumber convert the group member number to an index for field fieldi
func (cc *CrontabConfig) SetGroupNumber(state State, i, fieldi int, a *CrontabConstraint, s string) error {
k, err := strconv.Atoi(s)
if err != nil {
return err
}
if k < cc.Fields[fieldi].min || k > cc.Fields[fieldi].max {
return &ErrorBadIndex{FieldName: cc.Fields[fieldi].name, Value: k}
}
if state == StateInNumber {
*a = [3]int{k, k, 1}
} else if state == StateInEndRangeNumber {
(*a)[1] = k
} else if state == StateInStepNumber {
if k < 1 {
return &ErrorBadIndex{FieldName: cc.Fields[fieldi].name, Value: k}
}
(*a)[2] = k
} else {
return &ErrorParse{Index: i, State: state}
}
return nil
}
// Len crontab config is sortable
func (cc CrontabConfig) Len() int {
return len(cc.Fields)
}
// lookupNameIndex lookup index of s in ss
func lookupNameIndex(ss []string, s string) int {
q := -1
// apples to apples
s = strings.ToLower(s)
for i := range ss {
// go through all the names looking for a prefix match
if s == ss[i] {
// exact matches always result in an index
return i
} else if strings.HasPrefix(ss[i], s) {
// unambiguous prefix matches result in an index
if q >= 0 {
return -1
}
q = i
}
}
return q
}