Files
nib-v1/cmd/promote.go
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

61 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"github.com/lerko/nib/internal/carddata"
"github.com/lerko/nib/internal/db"
"github.com/lerko/nib/internal/display"
"github.com/spf13/cobra"
)
var promoteCmd = &cobra.Command{
Use: "promote <id> [type]",
Short: "promote a fluid entity to a card",
Args: cobra.RangeArgs(1, 2),
RunE: runPromote,
}
func init() {
rootCmd.AddCommand(promoteCmd)
}
func runPromote(cmd *cobra.Command, args []string) error {
store, err := openStore()
if err != nil {
return err
}
defer store.Close()
id, err := store.Resolve(cmd.Context(), args[0])
if err != nil {
return fmt.Errorf("not_found — no entity with id %s", args[0])
}
cardType := db.CardSnippet
if len(args) > 1 {
if !db.ValidCardType(args[1]) {
return fmt.Errorf("invalid_type — %q is not a valid card type", args[1])
}
cardType = db.CardType(args[1])
}
e, err := store.Get(cmd.Context(), id)
if err != nil {
return err
}
cd := carddata.GenerateCardData(cardType, e.Body)
if err := store.Promote(cmd.Context(), id, cardType, cd); err != nil {
if err == db.ErrAlreadyPromoted {
return fmt.Errorf("invalid_promote — entity %s is already a %s",
display.FormatID(id), *e.CardType)
}
return err
}
fmt.Printf("promoted %s → %s\n", display.FormatID(id), cardType)
return nil
}