ParseSchedule converts a human-friendly schedule expression into a cron expression Returns the cron expression and the original friendly format for comments
(input string)
| 25 | // ParseSchedule converts a human-friendly schedule expression into a cron expression |
| 26 | // Returns the cron expression and the original friendly format for comments |
| 27 | func ParseSchedule(input string) (cron string, original string, err error) { |
| 28 | scheduleLog.Printf("Parsing schedule expression: %s", input) |
| 29 | input = strings.TrimSpace(input) |
| 30 | if input == "" { |
| 31 | return "", "", errors.New("schedule expression cannot be empty") |
| 32 | } |
| 33 | |
| 34 | // If it's already a cron expression (5 fields separated by spaces), return as-is |
| 35 | if IsCronExpression(input) { |
| 36 | scheduleLog.Printf("Input is already a valid cron expression: %s", input) |
| 37 | return input, "", nil |
| 38 | } |
| 39 | |
| 40 | parser := &ScheduleParser{ |
| 41 | input: input, |
| 42 | } |
| 43 | |
| 44 | // Tokenize the input |
| 45 | if err := parser.tokenize(); err != nil { |
| 46 | scheduleLog.Printf("Tokenization failed: %s", err) |
| 47 | return "", "", err |
| 48 | } |
| 49 | |
| 50 | // Parse the tokens |
| 51 | cronExpr, err := parser.parse() |
| 52 | if err != nil { |
| 53 | scheduleLog.Printf("Parsing failed: %s", err) |
| 54 | return "", "", err |
| 55 | } |
| 56 | |
| 57 | scheduleLog.Printf("Successfully parsed schedule to cron: %s", cronExpr) |
| 58 | return cronExpr, input, nil |
| 59 | } |
| 60 | |
| 61 | // tokenize breaks the input into tokens |
| 62 | func (p *ScheduleParser) tokenize() error { |