-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrontab.go
124 lines (98 loc) · 2.8 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
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
package every
import (
"fmt"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
const (
crontabCommand = "crontab"
)
// WriteCrontab writes crontab
func WriteCrontab(config *Config) error {
crontab, err := readCrontab()
if err != nil {
return err
}
crontab, err = UpdateCrontab(crontab, config)
if err != nil {
return err
}
return writeCrontab(crontab)
}
// UpdateCrontab returns updated crontab content
func UpdateCrontab(crontab string, config *Config) (string, error) {
configPath, err := filepath.Abs(config.Path)
if err != nil {
return "", fmt.Errorf("can't get config absolute path: %v", err)
}
header := fmt.Sprintf("# Begin every generated jobs for %s", configPath)
footer := fmt.Sprintf("# End every generated jobs for %s", configPath)
reBlock, err := regexp.Compile(fmt.Sprintf(`(?m)^%s$(?:.*\n)+^%s$`, regexp.QuoteMeta(header), regexp.QuoteMeta(footer)))
if err != nil {
return "", fmt.Errorf("crontab regex error: %v", err)
}
buf := &strings.Builder{}
buf.WriteString(header)
buf.WriteString("\n")
for _, e := range config.Everies {
cronjob, err := e.Cronjob()
if err != nil {
return "", err
}
buf.WriteString(cronjob)
buf.WriteString("\n")
}
buf.WriteString(footer)
matched := reBlock.MatchString(crontab)
if matched {
crontab = reBlock.ReplaceAllString(crontab, buf.String())
return crontab, nil
}
crontab += "\n\n"
crontab += buf.String()
crontab += "\n\n"
return crontab, nil
}
// readCrontab reads crontab content using crontab command
func readCrontab() (string, error) {
args := []string{"-l"}
output, err := exec.Command(crontabCommand, args...).Output()
if err != nil {
return "", fmt.Errorf("can't read crontab: %v", err)
}
crontab := strings.Trim(string(output), "\n")
return crontab, nil
}
// writeCrontab writes crontab content using crontab command
func writeCrontab(content string) error {
cmd := exec.Command(crontabCommand)
cmd.Stdin = strings.NewReader(content)
if err := cmd.Run(); err != nil {
return fmt.Errorf("can't write crontab: %v", err)
}
return nil
}
// CleanCrontab cleanup current Everyfile config fron crontab
func CleanCrontab(config *Config) error {
crontab, err := readCrontab()
if err != nil {
return err
}
configPath, err := filepath.Abs(config.Path)
if err != nil {
return fmt.Errorf("can't get config absolute path: %v", err)
}
header := fmt.Sprintf("# Begin every generated jobs for %s", configPath)
footer := fmt.Sprintf("# End every generated jobs for %s", configPath)
reBlock, err := regexp.Compile(fmt.Sprintf(`(?m)^%s$(?:.*\n)+^%s$`, regexp.QuoteMeta(header), regexp.QuoteMeta(footer)))
if err != nil {
return fmt.Errorf("crontab regex error: %v", err)
}
matched := reBlock.MatchString(crontab)
if matched {
crontab = reBlock.ReplaceAllString(crontab, "")
}
return writeCrontab(crontab)
}