-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronentry.go
68 lines (61 loc) · 1.96 KB
/
cronentry.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
package main
import (
"fmt"
"strings"
"time"
)
// CronEntry represents one cron entry
type CronEntry struct {
spec string
chunks []string
nextRun time.Time
label string
}
// CronEntries is the list of CronEntry iterated upon by the script
var CronEntries []CronEntry = make([]CronEntry, 0)
// SchedulingPart returns the scheduling part of the CronEntry
func (c CronEntry) SchedulingPart() string {
return strings.Join(c.chunks[0:5], " ")
}
// CommandPart returns the command part of the CronEntry
func (c CronEntry) CommandPart() string {
if len(c.label) > 0 {
return c.label
}
if len(c.chunks) < 5 {
return ""
}
cmdPart := strings.Join(c.chunks[5:], " ")
if showRedirectDetails {
return cmdPart
}
cmdWithoutRedirects := cmdPart
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, "2> /dev/null", "", -1)
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, "2>/dev/null", "", -1)
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, "1> /dev/null", "", -1)
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, "1>/dev/null", "", -1)
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, "> /dev/null", "", -1)
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, ">/dev/null", "", -1)
cmdWithoutRedirects = strings.Replace(cmdWithoutRedirects, "2>&1", "", -1)
return cmdWithoutRedirects
}
// Show the information about a CronEntry, depending on flags
func (c CronEntry) Show() string {
chunks := make([]string, 0)
if showDeltaSeconds {
chunks = append(chunks, fmt.Sprintf("%d", c.nextRun.Unix()-now.Unix()))
}
if showDeltaCoarse {
chunks = append(chunks, secondsToCoarseHMS(c.nextRun.Unix()-now.Unix()))
} else if showDeltaHMS {
chunks = append(chunks, secondsToHMS(c.nextRun.Unix()-now.Unix()))
}
if showTimestamp {
chunks = append(chunks, c.nextRun.String())
}
if showCronSpec {
chunks = append(chunks, c.SchedulingPart())
}
chunks = append(chunks, c.CommandPart())
return strings.Join(chunks, "\t")
}