package cmd import ( "fmt" "os" "os/exec" "path/filepath" "strings" "git.jnss.me/joakim/jadedepo/internal/engine" "github.com/spf13/cobra" ) var ( addCmd = &cobra.Command{ Short: "Add note to depository", Use: "add [title]", Args: cobra.MinimumNArgs(1), Run: addNote, } ) func init() { rootCmd.AddCommand(addCmd) } func addNote(cmd *cobra.Command, args []string) { jd, err := engine.GetInstance() if err != nil { fmt.Fprintf(os.Stderr, "Error: %v\n", err) os.Exit(1) } // Join all args as the title title := strings.Join(args, " ") // Convert title to filename filename := engine.TitleToFilename(title) notePath := filepath.Join(jd.Config.DepoPath, filename) // Check if file already exists if _, err := os.Stat(notePath); err == nil { fmt.Fprintf(os.Stderr, "Error: note '%s' already exists\n", filename) os.Exit(1) } // Create file with initial heading initialContent := fmt.Sprintf("# %s\n\n", title) if err := os.WriteFile(notePath, []byte(initialContent), 0644); err != nil { fmt.Fprintf(os.Stderr, "Error creating note: %v\n", err) os.Exit(1) } // Open in $EDITOR 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("Created note: %s\n", filename) }