-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
181 lines (147 loc) · 4.02 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/csv"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/robfig/cron/v3"
)
func cronToHumanReadable(cronExpression string) (string, error) {
parser := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
schedule, err := parser.Parse(cronExpression)
if err != nil {
return "", err
}
// Get the next 5 scheduled times
var scheduleTimes []string
now := schedule.Next(time.Now())
for i := 0; i < 5; i++ {
nextTime := schedule.Next(now)
scheduleTimes = append(scheduleTimes, nextTime.Format("2006-01-02 15:04:05"))
now = nextTime
}
// Extract human-readable details
humanReadable := fmt.Sprintf("Expression: %s\n", cronExpression)
humanReadable += fmt.Sprintf("Next 5 scheduled times:\n%s\n", strings.Join(scheduleTimes, "\n"))
// Extract CRON fields
fields := strings.Fields(cronExpression)
if len(fields) < 5 {
return humanReadable, fmt.Errorf("Invalid CRON expression: %s", cronExpression)
}
mins, hours, dom, months, Dow := fields[0], fields[1], fields[2], fields[3], fields[4]
// Create human-readable description
description := fmt.Sprintf("It runs every %s", mins)
if mins != "*" {
description += " minute"
if mins != "*/1" {
description += "s"
}
}
if hours != "*" {
description += fmt.Sprintf(" past every %s", hours)
description += " hour"
if hours != "*/1" {
description += "s"
}
}
if dom != "*" {
description += fmt.Sprintf(" on every %s", dom)
description += " day"
if dom != "*/1" {
description += "s"
}
}
if months != "*" {
description += fmt.Sprintf(" of every %s", months)
description += " month"
if months != "*/1" {
description += "s"
}
}
if Dow != "*" {
description += fmt.Sprintf(" on every %s", Dow)
description += " day"
if Dow != "*/1" {
description += "s"
}
}
humanReadable += fmt.Sprintf("Human-readable description:\n%s\n", description)
return humanReadable, nil
}
func writeToCSV(filename string, data [][]string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
err = writer.WriteAll(data)
if err != nil {
return err
}
return nil
}
func main() {
var cronExpressions []string
var fromFile bool
flag.BoolVar(&fromFile, "file", false, "Read CRON expressions from file")
flag.Parse()
if fromFile {
// If -file flag is provided, read CRON expressions from file
fileName := "cron_expressions.txt"
file, err := os.Open(fileName)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
// Read the content from the file
buffer := make([]byte, 1024)
n, err := file.Read(buffer)
if err != nil {
fmt.Println("Error reading file:", err)
return
}
cronExpressions = strings.Fields(string(buffer[:n]))
} else {
// If -file flag is not provided, use command line arguments as CRON expressions
cronExpressions = flag.Args()
}
if len(cronExpressions) == 0 {
fmt.Println("Please provide CRON expressions using command line arguments or use -file flag to read from file.")
return
}
var csvData [][]string
for _, cronExpression := range cronExpressions {
humanReadable, err := cronToHumanReadable(cronExpression)
if err != nil {
fmt.Printf("Error parsing CRON expression '%s': %v\n", cronExpression, err)
continue
}
fmt.Println(humanReadable)
// Create CSV data
row := []string{"CRON Expression", "Next Scheduled Times"}
row = append(row, cronExpression)
schedule, err := cronToHumanReadable(cronExpression)
if err != nil {
fmt.Printf("Error parsing CRON expression '%s': %v\n", cronExpression, err)
continue
}
nextScheduledTimes := strings.Split(schedule, "\n")[2:]
// Append each row separately
for _, time := range nextScheduledTimes {
row = append(row, "", time)
}
csvData = append(csvData, row)
}
// Write to CSV file
err := writeToCSV("cron_schedule.csv", csvData)
if err != nil {
fmt.Println("Error writing to CSV:", err)
return
}
fmt.Println("CSV file created successfully.")
}