-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (57 loc) · 1.27 KB
/
main.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 (
"bufio"
"flag"
"fmt"
"log"
"os"
"regexp"
"sort"
"strings"
"time"
"github.com/robfig/cron"
)
const LAYOUT = "2006-01-02T15:04:05-0700"
func parseLines(from time.Time, sortFlag bool) ([]string, error) {
r := regexp.MustCompile(`^[\d/\-,*]`)
specParser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
var result []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if !r.MatchString(line) {
continue
}
def := strings.Fields(line)
sched, err := specParser.Parse(strings.Join(def[0:5], " "))
if err != nil {
return nil, err
}
next := sched.Next(from)
result = append(result, fmt.Sprintf("Next: %s # %s", next, line))
}
if err := scanner.Err(); err != nil {
return nil, err
}
if sortFlag {
sort.Strings(result)
}
return result, nil
}
func main() {
now := time.Now()
sortFlag := flag.Bool("sort", false, "Sort by next execution time.")
fromStr := flag.String("from", now.Format(LAYOUT), "Show TOC from its time.")
flag.Parse()
from, err := time.Parse(LAYOUT, *fromStr)
if err != nil {
log.Fatal(err)
}
result, err := parseLines(from, *sortFlag)
if err != nil {
log.Fatal(err)
}
for _, l := range result {
fmt.Println(l)
}
}