| 50 | } |
| 51 | |
| 52 | func runRm(_ *cobra.Command, args []string) error { |
| 53 | taskID := args[0] |
| 54 | |
| 55 | flags := GetGlobalFlags() |
| 56 | scanDir := ResolveScanDir(nil) |
| 57 | |
| 58 | taskScanner := scanner.NewScanner(scanDir, flags.Verbose, flags.IgnoreDirs) |
| 59 | result, err := taskScanner.Scan() |
| 60 | if err != nil { |
| 61 | return fmt.Errorf("scan failed: %w", err) |
| 62 | } |
| 63 | |
| 64 | task := findExactMatch(taskID, result.Tasks) |
| 65 | if task == nil { |
| 66 | return fmt.Errorf("task not found: %s", taskID) |
| 67 | } |
| 68 | |
| 69 | if dupes := findDuplicatesByID(taskID, result.Tasks); len(dupes) > 1 { |
| 70 | return fmt.Errorf("refusing to delete task %s: found %d files with this ID\n%s\nRun 'taskmd deduplicate' to fix", |
| 71 | taskID, len(dupes), formatDuplicatePaths(dupes)) |
| 72 | } |
| 73 | |
| 74 | if !rmForce { |
| 75 | if err := checkTaskReferences(taskID, result.Tasks); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | relPath := resolveRelPath(scanDir, task.FilePath) |
| 81 | |
| 82 | r := getRenderer() |
| 83 | fmt.Printf("Delete 1 task:\n") |
| 84 | fmt.Printf(" %s %s %s\n", formatTaskID(task.ID, r), task.Title, formatDim("("+relPath+")", r)) |
| 85 | |
| 86 | if rmDryRun { |
| 87 | fmt.Println("\n" + formatWarning("Dry run — no changes made.", r)) |
| 88 | return nil |
| 89 | } |
| 90 | |
| 91 | if !rmForce { |
| 92 | fmt.Printf("\nConfirm delete? [y/N] ") |
| 93 | reader := bufio.NewReader(rmStdinReader) |
| 94 | line, _ := reader.ReadString('\n') |
| 95 | line = strings.TrimSpace(strings.ToLower(line)) |
| 96 | if line != "y" && line != "yes" { |
| 97 | fmt.Println("Cancelled.") |
| 98 | return nil |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if err := os.Remove(task.FilePath); err != nil { |
| 103 | return fmt.Errorf("failed to delete %s: %w", relPath, err) |
| 104 | } |
| 105 | |
| 106 | cleanupWorklog(task.FilePath, task.ID) |
| 107 | |
| 108 | fmt.Println(formatSuccess("Deleted 1 task.", r)) |
| 109 | return nil |