| 456 | } |
| 457 | |
| 458 | func (l Line) Write() string { |
| 459 | var outputLines []string |
| 460 | var lineParts []string |
| 461 | |
| 462 | // Add the cronitor: ignore comment if present |
| 463 | if l.Ignored { |
| 464 | outputLines = append(outputLines, "# cronitor: ignore") |
| 465 | } |
| 466 | |
| 467 | // Add the name comment if present |
| 468 | if len(l.Name) > 0 { |
| 469 | outputLines = append(outputLines, fmt.Sprintf("# Name: %s", l.Name)) |
| 470 | } |
| 471 | |
| 472 | if !l.IsMonitorable() { |
| 473 | // If this is a comment line, ensure it starts with # |
| 474 | if l.IsComment && !strings.HasPrefix(l.FullLine, "#") { |
| 475 | lineParts = append(lineParts, "# "+l.FullLine) |
| 476 | } else { |
| 477 | lineParts = append(lineParts, l.FullLine) |
| 478 | } |
| 479 | } else { |
| 480 | // If this line is marked as a comment, ensure it is commented out in the crontab |
| 481 | if l.IsComment { |
| 482 | lineParts = append(lineParts, "#") |
| 483 | } |
| 484 | |
| 485 | lineParts = append(lineParts, l.CronExpression) |
| 486 | |
| 487 | if !l.Crontab.IsUserCrontab { |
| 488 | lineParts = append(lineParts, l.RunAs) |
| 489 | } |
| 490 | |
| 491 | if code := l.GetCode(); code != "" { |
| 492 | lineParts = append(lineParts, "cronitor") |
| 493 | |
| 494 | // Add the --env flag if environment is set |
| 495 | if env := viper.GetString("CRONITOR_ENV"); env != "" { |
| 496 | lineParts = append(lineParts, "--env") |
| 497 | lineParts = append(lineParts, env) |
| 498 | } |
| 499 | |
| 500 | if l.Mon.NoStdoutPassthru { |
| 501 | lineParts = append(lineParts, "--no-stdout") |
| 502 | } |
| 503 | lineParts = append(lineParts, "exec") |
| 504 | lineParts = append(lineParts, code) |
| 505 | |
| 506 | if len(l.CommandToRun) > 0 { |
| 507 | if l.CommandIsComplex() { |
| 508 | lineParts = append(lineParts, "\""+strings.Replace(l.CommandToRun, "\"", "\\\"", -1)+"\"") |
| 509 | } else { |
| 510 | lineParts = append(lineParts, l.CommandToRun) |
| 511 | } |
| 512 | } |
| 513 | } else { |
| 514 | lineParts = append(lineParts, l.CommandToRun) |
| 515 | } |