-
Notifications
You must be signed in to change notification settings - Fork 0
/
crontab.go
51 lines (39 loc) · 1.05 KB
/
crontab.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
package main
import "regexp"
type CrontabEntry struct {
Schedule string
Command string
}
type ServerCrontab struct {
Server string
User string
rawCrontab string
Entries []CrontabEntry
}
type ServerCrontabs []ServerCrontab
func (self ServerCrontabs) Len() int {
return len(self)
}
func (self ServerCrontabs) Less(i, j int) bool {
return self[i].Server < self[j].Server
}
func (self ServerCrontabs) Swap(i, j int) {
self[i], self[j] = self[j], self[i]
}
func (self *ServerCrontab) parseEntries() {
regexp := regexp.MustCompile(`(?m)^(@[a-z]+|([0-9\*\/\-\,]+ [0-9\*\/\-\,]+ [0-9\*\/\-\,\?LW]+ [0-9A-Z\*\/\-\,]+ [0-9A-Z\*\/\-\,\?L\#]+[ 0-9\*\/\-\,]*)) (.*)$`)
result := regexp.FindAllStringSubmatch(self.rawCrontab, -1)
if self.Entries == nil {
self.Entries = []CrontabEntry{}
}
for _, x := range result {
// 0: 0 3 * * 1 cmd
// 1-2: 0 3 * * 1
// 3: cmd
if len(x) != 4 {
halt("crontab format could not be detected")
}
entry := CrontabEntry{Schedule: x[1], Command: x[3]}
self.Entries = append(self.Entries, entry)
}
}