Files
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

54 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"github.com/lerko/nib/internal/db"
"github.com/lerko/nib/internal/display"
"github.com/spf13/cobra"
)
var absorbCmd = &cobra.Command{
Use: "absorb <target> <source>",
Short: "pull source material into target, ghost the source",
Args: cobra.ExactArgs(2),
RunE: runAbsorb,
}
func init() {
rootCmd.AddCommand(absorbCmd)
}
func runAbsorb(cmd *cobra.Command, args []string) error {
store, err := openStore()
if err != nil {
return err
}
defer store.Close()
targetID, err := store.Resolve(cmd.Context(), args[0])
if err != nil {
return fmt.Errorf("not_found — no entity with id %s", args[0])
}
sourceID, err := store.Resolve(cmd.Context(), args[1])
if err != nil {
return fmt.Errorf("not_found — no entity with id %s", args[1])
}
if targetID == sourceID {
return fmt.Errorf("target and source must be different entities")
}
if err := store.Absorb(cmd.Context(), targetID, sourceID); err != nil {
if err == db.ErrTargetCrystallized {
return fmt.Errorf("invalid_absorb — target %s is crystallized, demote first",
display.FormatID(targetID))
}
return err
}
fmt.Printf("absorbed %s → %s\n", display.FormatID(sourceID), display.FormatID(targetID))
return nil
}