this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

crun: add thing

+109
+109
crun/main.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "io" 6 + "os" 7 + "strconv" 8 + "strings" 9 + ) 10 + 11 + func main() { 12 + if err := realMain(os.Args, os.Stdout); err != nil { 13 + fmt.Fprintln(os.Stderr, err) 14 + os.Exit(1) 15 + } 16 + } 17 + 18 + func realMain(args []string, stdout io.Writer) error { 19 + if len(args) != 2 { 20 + return fmt.Errorf("usage: %s <cron expression>", args[0]) 21 + } 22 + 23 + cron := args[1] 24 + 25 + description, err := DescribeCron(cron) 26 + if err != nil { 27 + return err 28 + } 29 + 30 + fmt.Fprintln(stdout, description) 31 + 32 + return nil 33 + } 34 + 35 + func DescribeCron(cron string) (string, error) { 36 + fields := strings.Fields(cron) 37 + 38 + if len(fields) != 5 { 39 + return "", fmt.Errorf("cron expression should have 5 fields") 40 + } 41 + 42 + minutes := fields[0] 43 + hours := fields[1] 44 + dayOfMonth := fields[2] 45 + month := fields[3] 46 + dayOfWeek := fields[4] 47 + 48 + switch { 49 + case minutes == "*" && hours == "*" && dayOfMonth == "*" && month == "*" && dayOfWeek == "*": 50 + return "This runs every minute.", nil 51 + case minutes == "*/15" && hours == "*" && dayOfMonth == "*" && month == "*" && dayOfWeek == "*": 52 + return "This runs every 15 minutes.", nil 53 + case minutes == "0" && hours == "*" && dayOfMonth == "*" && month == "*" && dayOfWeek == "*": 54 + return "This runs at the start of every hour.", nil 55 + case minutes == "0" && hours == "*/3" && dayOfMonth == "*" && month == "*" && dayOfWeek == "*": 56 + return "This runs every 3 hours.", nil 57 + case minutes == "0" && hours == "0" && dayOfMonth == "*" && month == "*" && dayOfWeek == "*": 58 + return "This runs every day at midnight.", nil 59 + case minutes == "0" && hours == "0" && dayOfMonth == "1" && month == "*" && dayOfWeek == "*": 60 + return "This runs every month at midnight on the first day.", nil 61 + case minutes == "0" && hours == "0" && dayOfMonth == "1" && month == "1" && dayOfWeek == "*": 62 + return "This runs every year at midnight on January 1.", nil 63 + } 64 + 65 + // Otherwise, construct the description 66 + description := "This runs " 67 + 68 + // Minutes 69 + if minutes == "*" { 70 + description += "every minute " 71 + } else { 72 + min, _ := strconv.Atoi(minutes) 73 + description += fmt.Sprintf("at %d minutes past the hour ", min) 74 + } 75 + 76 + // Hours 77 + if hours == "*" { 78 + description += "of every hour " 79 + } else { 80 + hr, _ := strconv.Atoi(hours) 81 + description += fmt.Sprintf("at %02d:00 ", hr) 82 + } 83 + 84 + // Day of Month 85 + if dayOfMonth == "*" { 86 + description += "on every day " 87 + } else { 88 + dom, _ := strconv.Atoi(dayOfMonth) 89 + description += fmt.Sprintf("on the %d of the month ", dom) 90 + } 91 + 92 + // Month 93 + if month == "*" { 94 + description += "of every month " 95 + } else { 96 + m, _ := strconv.Atoi(month) 97 + description += fmt.Sprintf("in month %d ", m) 98 + } 99 + 100 + // Day of Week 101 + if dayOfWeek == "*" { 102 + description += "and on every day of the week." 103 + } else { 104 + dow, _ := strconv.Atoi(dayOfWeek) 105 + description += fmt.Sprintf("and on the %d day of the week.", dow) 106 + } 107 + 108 + return description, nil 109 + }