Implement Jade CLI v1.0 MVP
Complete implementation of note management CLI with all core features: Commands: - add: Create new notes in $EDITOR with auto-generated filenames - list: Display all notes with titles, paths, and tags - search: Full-text search via ripgrep, tag-based filtering - tags: List all tags with occurrence counts - edit: Fuzzy search and edit notes by title - rm: Move notes to trash with confirmation prompt Features: - Automatic depository structure initialization (.jade/trash/) - Configurable tag prefix (default '+') - Parse title from first # heading (filename fallback) - Extract tags anywhere in content - Parse both [[wiki-links]] and [markdown](links) - Trash system with timestamps to prevent conflicts Technical: - Global config at ~/.config/jade/config.yml - Per-depository settings support - Ripgrep integration for fast search - $EDITOR integration for note editing - Comprehensive README with usage examples
This commit is contained in:
+49
-3
@@ -2,14 +2,20 @@ 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 [note]",
|
||||
Use: "add [title]",
|
||||
Args: cobra.MinimumNArgs(1),
|
||||
Run: addNote,
|
||||
}
|
||||
)
|
||||
@@ -19,7 +25,47 @@ func init() {
|
||||
}
|
||||
|
||||
func addNote(cmd *cobra.Command, args []string) {
|
||||
// open new note in $EDITOR
|
||||
jd, err := engine.GetInstance()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
fmt.Println(args)
|
||||
// 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)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.jnss.me/joakim/jadedepo/internal/engine"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
listCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all notes in the depository",
|
||||
Run: listNotes,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
}
|
||||
|
||||
func listNotes(cmd *cobra.Command, args []string) {
|
||||
jd, err := engine.GetInstance()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
notes, err := jd.ListAllNotes()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error listing notes: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(notes) == 0 {
|
||||
fmt.Println("No notes found in depository")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Found %d note(s):\n\n", len(notes))
|
||||
for _, note := range notes {
|
||||
fmt.Printf(" %s\n", note.Title)
|
||||
fmt.Printf(" Path: %s\n", note.Path)
|
||||
if len(note.Tags) > 0 {
|
||||
fmt.Printf(" Tags: %v\n", note.Tags)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.jnss.me/joakim/jadedepo/internal/engine"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
searchTag string
|
||||
|
||||
searchCmd = &cobra.Command{
|
||||
Use: "search [query]",
|
||||
Short: "Search notes by content or tags",
|
||||
Run: searchNotes,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
searchCmd.Flags().StringVarP(&searchTag, "tag", "t", "", "Search by tag")
|
||||
rootCmd.AddCommand(searchCmd)
|
||||
}
|
||||
|
||||
func searchNotes(cmd *cobra.Command, args []string) {
|
||||
jd, err := engine.GetInstance()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var results []string
|
||||
|
||||
// Search by tag if flag is provided
|
||||
if searchTag != "" {
|
||||
results, err = jd.SearchByTag(searchTag)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error searching by tag: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
// Search by content
|
||||
if len(args) == 0 {
|
||||
fmt.Fprintf(os.Stderr, "Error: query required when not searching by tag\n")
|
||||
os.Exit(1)
|
||||
}
|
||||
query := strings.Join(args, " ")
|
||||
results, err = jd.SearchContent(query)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error searching: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
fmt.Println("No matches found")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Found %d match(es):\n\n", len(results))
|
||||
for _, path := range results {
|
||||
// Load note to get title
|
||||
note, err := engine.LoadNote(jd.Config.DepoPath, path, jd.Config.TagPrefix)
|
||||
if err != nil {
|
||||
fmt.Printf(" %s\n", path)
|
||||
continue
|
||||
}
|
||||
fmt.Printf(" %s (%s)\n", note.Title, path)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.jnss.me/joakim/jadedepo/internal/engine"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
tagsCmd = &cobra.Command{
|
||||
Use: "tags",
|
||||
Short: "List all tags with their counts",
|
||||
Run: listTags,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(tagsCmd)
|
||||
}
|
||||
|
||||
func listTags(cmd *cobra.Command, args []string) {
|
||||
jd, err := engine.GetInstance()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
tags, err := jd.ListAllTags()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error listing tags: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if len(tags) == 0 {
|
||||
fmt.Println("No tags found")
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Found %d tag(s):\n\n", len(tags))
|
||||
for _, tag := range tags {
|
||||
fmt.Printf(" %-20s %d note(s)\n", tag.Tag, tag.Count)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user