1d87d93172
- Implement global CLI config at ~/.config/jade/config.yml - Add jade depo commands (add, list, remove, set-default) - Support depository short names and context-aware detection - Remove tag_prefix config, hardcode + syntax for consistency - Update depository resolution: flag -> context -> default - Auto-initialize .jade/ directory structure when adding depos - Update documentation with new multi-depository workflow
115 lines
3.0 KiB
Go
115 lines
3.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.jnss.me/joakim/jadedepo/internal/engine"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
forceDelete bool
|
|
permanentDelete 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")
|
|
rmCmd.Flags().BoolVarP(&permanentDelete, "permanent", "p", false, "Delete permanently")
|
|
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]
|
|
}
|
|
|
|
notePath := filepath.Join(jd.Config.DepoPath, noteToDelete.Path)
|
|
|
|
// Confirm deletion
|
|
if !forceDelete {
|
|
if permanentDelete {
|
|
fmt.Printf("Are you sure you want to PERMANENTLY delete '%s'? This cannot be undone. (y/N): ", noteToDelete.Title)
|
|
} else {
|
|
fmt.Printf("Are you sure you want to move '%s' to trash? (y/N): ", noteToDelete.Title)
|
|
}
|
|
var confirm string
|
|
fmt.Scanln(&confirm)
|
|
if strings.ToLower(confirm) != "y" && strings.ToLower(confirm) != "yes" {
|
|
fmt.Println("Deletion cancelled")
|
|
return
|
|
}
|
|
}
|
|
|
|
if permanentDelete {
|
|
// Permanently delete the file
|
|
if err := os.Remove(notePath); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error deleting note: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("Permanently deleted '%s'\n", noteToDelete.Title)
|
|
} else {
|
|
// Move to trash
|
|
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)
|
|
}
|
|
}
|