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 [type]", Short: "promote a fluid entity to a card", Args: cobra.RangeArgs(1, 2), RunE: runPromote, } func init() { rootCmd.AddCommand(promoteCmd) } func runPromote(_ *cobra.Command, args []string) error { store, err := openStore() if err != nil { return err } defer store.Close() id, err := store.Resolve(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(id) if err != nil { return err } cd := carddata.GenerateCardData(cardType, e.Body) if err := store.Promote(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 }