RemoveWorkflows removes workflows matching a pattern
(pattern string, keepOrphans bool, workflowDir string)
| 19 | |
| 20 | // RemoveWorkflows removes workflows matching a pattern |
| 21 | func RemoveWorkflows(pattern string, keepOrphans bool, workflowDir string) error { |
| 22 | removeLog.Printf("Removing workflows: pattern=%q, keepOrphans=%v, workflowDir=%q", pattern, keepOrphans, workflowDir) |
| 23 | workflowsDir := workflowDir |
| 24 | if workflowsDir == "" { |
| 25 | workflowsDir = getWorkflowsDir() |
| 26 | } |
| 27 | |
| 28 | if _, err := os.Stat(workflowsDir); os.IsNotExist(err) { |
| 29 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No .github/workflows directory found.")) |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | // Find all markdown files in .github/workflows |
| 34 | mdFiles, err := filepath.Glob(filepath.Join(workflowsDir, "*.md")) |
| 35 | if err != nil { |
| 36 | return fmt.Errorf("failed to find workflow files: %w", err) |
| 37 | } |
| 38 | |
| 39 | // Filter out README.md files |
| 40 | mdFiles = filterWorkflowFiles(mdFiles) |
| 41 | |
| 42 | removeLog.Printf("Found %d workflow files", len(mdFiles)) |
| 43 | if len(mdFiles) == 0 { |
| 44 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("No workflow files found to remove.")) |
| 45 | return nil |
| 46 | } |
| 47 | |
| 48 | var filesToRemove []string |
| 49 | |
| 50 | // If no pattern specified, list all files for user to see |
| 51 | if pattern == "" { |
| 52 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage("Available workflows to remove:")) |
| 53 | for _, file := range mdFiles { |
| 54 | workflowName, _ := extractWorkflowNameFromFile(file) |
| 55 | base := filepath.Base(file) |
| 56 | name := normalizeWorkflowID(base) |
| 57 | if workflowName != "" { |
| 58 | fmt.Fprintf(os.Stderr, " %-20s - %s\n", name, workflowName) |
| 59 | } else { |
| 60 | fmt.Fprintf(os.Stderr, " %s\n", name) |
| 61 | } |
| 62 | } |
| 63 | fmt.Fprintln(os.Stderr, console.FormatInfoMessage(fmt.Sprintf("\nUsage: %s remove <filter>", string(constants.CLIExtensionPrefix)))) |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | // Find matching files by workflow name or filename |
| 68 | for _, file := range mdFiles { |
| 69 | base := filepath.Base(file) |
| 70 | filename := normalizeWorkflowID(base) |
| 71 | workflowName, _ := extractWorkflowNameFromFile(file) |
| 72 | |
| 73 | // Check if pattern matches filename or workflow name |
| 74 | if strings.Contains(strings.ToLower(filename), strings.ToLower(pattern)) || |
| 75 | strings.Contains(strings.ToLower(workflowName), strings.ToLower(pattern)) { |
| 76 | filesToRemove = append(filesToRemove, file) |
| 77 | } |
| 78 | } |