Files
nib-v1/cmd/add.go
T
lerko d715b053e7 refactor(db): thread context.Context through all Store methods
Enables request-scoped cancellation, timeouts, and graceful shutdown
for all database operations across API handlers, CLI commands, and TUI.
2026-05-20 20:51:51 -04:00

78 lines
1.6 KiB
Go

package cmd
import (
"fmt"
"strings"
"github.com/lerko/nib/internal/db"
"github.com/lerko/nib/internal/display"
"github.com/lerko/nib/internal/parse"
"github.com/spf13/cobra"
)
var addCmd = &cobra.Command{
Use: "add [input]",
Short: "capture a new entity",
Args: cobra.MinimumNArgs(1),
RunE: runAdd,
}
func runAdd(cmd *cobra.Command, args []string) error {
input := strings.Join(args, " ")
parsed, err := parse.Parse(input)
if err != nil {
return fmt.Errorf("parse: %w", err)
}
store, err := openStore()
if err != nil {
return err
}
defer store.Close()
e := &db.Entity{
Body: parsed.Body,
Title: parsed.Title,
Description: parsed.Description,
Glyph: db.Glyph(parsed.Glyph),
Tags: parsed.Tags,
Pinned: parsed.Pin,
}
if parsed.TimeAnchor != nil {
e.TimeAnchor = parsed.TimeAnchor
}
if parsed.CardSuffix != nil {
ct := db.CardType(*parsed.CardSuffix)
e.CardType = &ct
}
if err := store.Create(cmd.Context(), e); err != nil {
return err
}
glyph := display.DisplayGlyph(e.Glyph, e.CardType)
shortID := display.FormatID(e.ID)
var parts []string
parts = append(parts, glyph)
if e.Title != nil {
parts = append(parts, " "+*e.Title)
} else {
parts = append(parts, " "+e.Body)
}
if e.TimeAnchor != nil {
parts = append(parts, " @"+*e.TimeAnchor)
}
for _, tag := range e.Tags {
parts = append(parts, " #"+tag)
}
parts = append(parts, " ["+shortID+"]")
if e.CardType != nil {
parts = append(parts, fmt.Sprintf(" (%s)", *e.CardType))
}
fmt.Println(strings.Join(parts, ""))
return nil
}