|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/araddon/dateparse" |
| 13 | + "github.com/winebarrel/cronplan" |
| 14 | +) |
| 15 | + |
| 16 | +func init() { |
| 17 | + log.SetFlags(0) |
| 18 | +} |
| 19 | + |
| 20 | +func main() { |
| 21 | + flags := parseFlags() |
| 22 | + |
| 23 | + var scanner *bufio.Scanner |
| 24 | + |
| 25 | + if flags.file == "-" { |
| 26 | + scanner = bufio.NewScanner(os.Stdin) |
| 27 | + } else { |
| 28 | + file, err := os.Open(flags.file) |
| 29 | + |
| 30 | + if err != nil { |
| 31 | + log.Fatal(err) |
| 32 | + } |
| 33 | + |
| 34 | + defer file.Close() |
| 35 | + |
| 36 | + scanner = bufio.NewScanner(file) |
| 37 | + } |
| 38 | + |
| 39 | + type exprNext struct { |
| 40 | + expr string |
| 41 | + next time.Time |
| 42 | + } |
| 43 | + |
| 44 | + schedule := []exprNext{} |
| 45 | + |
| 46 | + for scanner.Scan() { |
| 47 | + expr := scanner.Text() |
| 48 | + expr = strings.TrimSpace(expr) |
| 49 | + expr = strings.TrimPrefix(expr, "cron(") |
| 50 | + expr = strings.TrimSuffix(expr, ")") |
| 51 | + |
| 52 | + cron, err := cronplan.Parse(expr) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + var start, end time.Time |
| 59 | + |
| 60 | + if flags.start == "" { |
| 61 | + now := time.Now() |
| 62 | + start = time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()) |
| 63 | + } else { |
| 64 | + start, err = dateparse.ParseAny(flags.start) |
| 65 | + |
| 66 | + if err != nil { |
| 67 | + log.Fatal(err) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if flags.end == "" { |
| 72 | + end = time.Date(start.Year(), start.Month(), start.Day(), 23, 59, 50, 0, start.Location()) |
| 73 | + } |
| 74 | + |
| 75 | + nexts := cron.Between(start, end) |
| 76 | + |
| 77 | + for _, n := range nexts { |
| 78 | + schedule = append(schedule, exprNext{expr: expr, next: n}) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + sort.Slice(schedule, func(i, j int) bool { |
| 83 | + return schedule[i].next.Before(schedule[j].next) |
| 84 | + }) |
| 85 | + |
| 86 | + for _, ln := range schedule { |
| 87 | + fmt.Printf("%s\t%s\n", ln.next.Format("Mon, 02 Jan 2006 15:04:05"), ln.expr) |
| 88 | + } |
| 89 | +} |
0 commit comments