(noAutoDiscover bool)
| 102 | } |
| 103 | |
| 104 | func (c *Crontab) Parse(noAutoDiscover bool) (error, int) { |
| 105 | lines, errCode, err := c.load() |
| 106 | if err != nil { |
| 107 | return err, errCode |
| 108 | } |
| 109 | |
| 110 | if len(c.Lines) > 0 { |
| 111 | panic("Cannot read into non-empty crontab struct") |
| 112 | } |
| 113 | |
| 114 | var autoDiscoverLine *Line |
| 115 | var name string |
| 116 | var ignored bool |
| 117 | |
| 118 | for lineIndex, fullLine := range lines { |
| 119 | lineNumber := lineIndex + 1 // Convert to 1-indexed |
| 120 | var cronExpression string |
| 121 | var command []string |
| 122 | var runAs string |
| 123 | |
| 124 | fullLine = strings.TrimSpace(fullLine) |
| 125 | isComment := false |
| 126 | |
| 127 | // Check for special Name: comment, handle other comments normally |
| 128 | if nameMatch := regexp.MustCompile(`^#\s*Name:\s*(.+)$`).FindStringSubmatch(fullLine); nameMatch != nil { |
| 129 | name = strings.TrimSpace(nameMatch[1]) |
| 130 | continue |
| 131 | } |
| 132 | |
| 133 | // Check for special cronitor: ignore comment |
| 134 | if ignoreMatch := regexp.MustCompile(`^#\s*cronitor:\s*ignore\s*$`).FindStringSubmatch(fullLine); ignoreMatch != nil { |
| 135 | ignored = true |
| 136 | continue |
| 137 | } |
| 138 | |
| 139 | // If the line is a comment, check if it's a disabled job or just a regular comment |
| 140 | if strings.HasPrefix(fullLine, "#") { |
| 141 | commentContent := strings.TrimSpace(strings.TrimPrefix(fullLine, "#")) |
| 142 | |
| 143 | // Only treat as a potential disabled job if it looks like a valid cron expression |
| 144 | if isValidCronExpression(commentContent) { |
| 145 | fullLine = commentContent |
| 146 | isComment = true |
| 147 | } else { |
| 148 | // This is just a regular comment, create a comment line and continue |
| 149 | line := Line{ |
| 150 | IsComment: true, |
| 151 | IsJob: false, |
| 152 | Name: name, |
| 153 | FullLine: strings.TrimSpace(strings.TrimPrefix(fullLine, "#")), |
| 154 | LineNumber: lineNumber, |
| 155 | Ignored: ignored, |
| 156 | Crontab: c.lightweightCopy(), |
| 157 | } |
| 158 | |
| 159 | // Reset the name and ignored for the next line |
| 160 | if name != "" { |
| 161 | name = "" |
no test coverage detected