package cmd import ( "fmt" "os" "os/exec" "path/filepath" "strings" "git.jnss.me/joakim/jadedepo/internal/engine" "github.com/spf13/cobra" ) var ( editCmd = &cobra.Command{ Use: "edit [title]", Short: "Edit a note by searching for its title", Args: cobra.MinimumNArgs(1), Run: editNote, } ) func init() { rootCmd.AddCommand(editCmd) } func editNote(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 noteToEdit *engine.Note if len(matches) == 1 { noteToEdit = 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) } noteToEdit = matches[selection-1] } // Open note in editor notePath := filepath.Join(jd.Config.DepoPath, noteToEdit.Path) editor := os.Getenv("EDITOR") if editor == "" { editor = "vi" // fallback } editorCmd := exec.Command(editor, notePath) editorCmd.Stdin = os.Stdin editorCmd.Stdout = os.Stdout editorCmd.Stderr = os.Stderr if err := editorCmd.Run(); err != nil { fmt.Fprintf(os.Stderr, "Error opening editor: %v\n", err) os.Exit(1) } fmt.Printf("Edited note: %s\n", noteToEdit.Title) }