-
Notifications
You must be signed in to change notification settings - Fork 0
/
goat.go
75 lines (59 loc) · 1.86 KB
/
goat.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
package goat
import (
"errors"
"fmt"
"io"
"os/exec"
"regexp"
"strconv"
"time"
)
// AddJob adds a command to an at(1) execution queue. It will be run at atTime.
// Optionally, the job can be written to specific queue toQueue, default is "a".
// Queues must be adressed by a single letter.
func AddJob(command string, atTime time.Time, toQueue ...string) (int, error) {
atCmd := exec.Command("at", "-t", atTime.Format("200601021504"))
if len(toQueue) > 0 {
atCmd.Args = append(atCmd.Args, "-q", fmt.Sprintf("%c", toQueue[0][0]))
}
atStdin, _ := atCmd.StdinPipe()
_, _ = io.WriteString(atStdin, command)
atStdin.Close()
atStdout, err := atCmd.CombinedOutput()
if err != nil {
return -1, errors.New("could not add job")
}
outputTokens := regexp.MustCompile(`job (\d+) at`).FindStringSubmatch(string(atStdout))
if outputTokens == nil {
return -1, errors.New("job has been added, but did not get job id")
}
jobID, _ := strconv.Atoi(outputTokens[1])
return jobID, nil
}
// RemoveJob removes the job specified by jobID from the at(1) execution queues.
func RemoveJob(jobID int) error {
if err := exec.Command("atrm", strconv.Itoa(jobID)).Run(); err != nil {
return errors.New("could not delete job")
}
return nil
}
// ClearQueue removes all jobs from the at queue specified by queueLetter.
// If no queueLetter is given, clears default queue "a".
func ClearQueue(queueLetter ...string) error {
if len(queueLetter) == 0 {
queueLetter = []string{"a"}
}
atqCmd := exec.Command("atq", "-q", fmt.Sprintf("%c", queueLetter[0][0]))
atqStdout, err := atqCmd.Output()
if err != nil {
return errors.New("could not get job IDs")
}
jobIDs := regexp.MustCompile(`(?m:^\d+)`).FindAllStringSubmatch(string(atqStdout), -1)
for _, idToken := range jobIDs {
jobID, _ := strconv.Atoi(idToken[0])
if err := RemoveJob(jobID); err != nil {
return err
}
}
return nil
}