package cmd import ( "fmt" "os" "path/filepath" "strings" "time" "git.jnss.me/joakim/jadedepo/internal/engine" "github.com/spf13/cobra" ) var ( forceDelete bool rmCmd = &cobra.Command{ Use: "rm [title]", Short: "Remove a note (moves to trash)", Args: cobra.MinimumNArgs(1), Run: removeNote, } ) func init() { rmCmd.Flags().BoolVarP(&forceDelete, "force", "f", false, "Skip confirmation prompt") rootCmd.AddCommand(rmCmd) } func removeNote(cmd *cobra.Command, args []string) { jd, err := engine.GetInstance() if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } // Search for note by title searchTitle := strings.Join(args, " ") matches, err := jd.FindNoteByTitle(searchTitle) if err != nil { fmt.Fprintf(os.Stderr, "Error searching for note: %v\n", err) os.Exit(1) } if len(matches) == 0 { fmt.Fprintf(os.Stderr, "No notes found matching '%s'\n", searchTitle) os.Exit(1) } var noteToDelete *engine.Note if len(matches) == 1 { noteToDelete = matches[0] } else { // Multiple matches - show options fmt.Printf("Multiple notes found matching '%s':\n\n", searchTitle) for i, note := range matches { fmt.Printf(" %d. %s (%s)\n", i+1, note.Title, note.Path) } fmt.Print("\nSelect note number (1-", len(matches), "): ") var selection int _, err := fmt.Scanf("%d", &selection) if err != nil || selection < 1 || selection > len(matches) { fmt.Fprintf(os.Stderr, "Invalid selection\n") os.Exit(1) } noteToDelete = matches[selection-1] } // Confirm deletion if !forceDelete { fmt.Printf("Are you sure you want to delete '%s'? (y/N): ", noteToDelete.Title) var confirm string fmt.Scanln(&confirm) if strings.ToLower(confirm) != "y" && strings.ToLower(confirm) != "yes" { fmt.Println("Deletion cancelled") return } } // Move to trash notePath := filepath.Join(jd.Config.DepoPath, noteToDelete.Path) trashPath := jd.GetTrashPath() // Create unique filename in trash (add timestamp to avoid conflicts) timestamp := time.Now().Format("20060102-150405") trashFilename := fmt.Sprintf("%s-%s", timestamp, filepath.Base(noteToDelete.Path)) trashDestination := filepath.Join(trashPath, trashFilename) if err := os.Rename(notePath, trashDestination); err != nil { fmt.Fprintf(os.Stderr, "Error moving note to trash: %v\n", err) os.Exit(1) } fmt.Printf("Moved '%s' to trash\n", noteToDelete.Title) fmt.Printf("Trash location: %s\n", trashDestination) }