| 578 | } |
| 579 | |
| 580 | func createDefaultName(line *lib.Line, crontab *lib.Crontab, effectiveHostname string, excludeFromName []string, allNameCandidates map[string]bool) string { |
| 581 | excludeFromName = append(excludeFromName, "2>&1") |
| 582 | excludeFromName = append(excludeFromName, "/bin/bash -l -c") |
| 583 | excludeFromName = append(excludeFromName, "/bin/bash -lc") |
| 584 | excludeFromName = append(excludeFromName, "/bin/bash -c -l") |
| 585 | excludeFromName = append(excludeFromName, "/bin/bash -cl") |
| 586 | excludeFromName = append(excludeFromName, "/dev/null") |
| 587 | |
| 588 | excludeFromName = append(excludeFromName, "'") |
| 589 | excludeFromName = append(excludeFromName, "\"") |
| 590 | excludeFromName = append(excludeFromName, "\\") |
| 591 | |
| 592 | // Limit the visible hostname portion to 21 chars |
| 593 | formattedHostname := "" |
| 594 | if effectiveHostname != "" { |
| 595 | if len(effectiveHostname) > 21 { |
| 596 | effectiveHostname = fmt.Sprintf("%s...%s", effectiveHostname[:9], effectiveHostname[len(effectiveHostname)-9:]) |
| 597 | } |
| 598 | formattedHostname = fmt.Sprintf("[%s] ", effectiveHostname) |
| 599 | } |
| 600 | |
| 601 | if line.IsAutoDiscoverCommand() { |
| 602 | return truncateString(fmt.Sprintf("%sAuto discover %s", formattedHostname, strings.TrimSpace(crontab.DisplayName())), maxNameLen) |
| 603 | } |
| 604 | |
| 605 | // Remove output redirection |
| 606 | CommandToRun := line.CommandToRun |
| 607 | for _, redirectionOperator := range []string{">>", ">"} { |
| 608 | if operatorPosition := strings.LastIndex(line.CommandToRun, redirectionOperator); operatorPosition > 0 { |
| 609 | CommandToRun = CommandToRun[:operatorPosition] |
| 610 | break |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | // Remove exclusion text |
| 615 | for _, substr := range excludeFromName { |
| 616 | CommandToRun = strings.Replace(CommandToRun, substr, "", -1) |
| 617 | } |
| 618 | |
| 619 | CommandToRun = strings.Join(strings.Fields(CommandToRun), " ") |
| 620 | |
| 621 | // Assemble the candidate name. |
| 622 | // Ensure we don't produce dupe names if the user has same command on multiple lines. |
| 623 | formattedRunAs := "" |
| 624 | lineNumSuffix := fmt.Sprintf(" L%d", line.LineNumber) |
| 625 | if line.RunAs != "" { |
| 626 | formattedRunAs = fmt.Sprintf("%s ", line.RunAs) |
| 627 | } |
| 628 | |
| 629 | candidate := formattedHostname + formattedRunAs + CommandToRun |
| 630 | |
| 631 | if _, exists := allNameCandidates[candidate]; exists { |
| 632 | candidate = fmt.Sprintf("%s%s", candidate, lineNumSuffix) |
| 633 | } else { |
| 634 | allNameCandidates[candidate] = true |
| 635 | } |
| 636 | |
| 637 | // Return if short, truncate if necessary. |