Files
2026-08-21 16:30:49 +05:00

60 lines
1.3 KiB
Go

package cmd
import (
"fmt"
"github.com/abelith/spellbook/internal/errors"
"github.com/abelith/spellbook/internal/models"
"github.com/spf13/cobra"
"strings"
)
var reservedCommands = map[string]struct{}{
"add": {},
"rm": {},
"help": {},
"completion": {},
"search": {},
"export": {},
"import": {},
}
func NewAddCommand(app Application) *cobra.Command {
spell := models.Spell{}
cmd := &cobra.Command{
Use: "add [flags] -- [spell]",
Short: "Add new spell",
Long: `WIP`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) < 1 {
return errors.ErrInsufficientArgs
}
if isReservedCommand(args[0]) {
return fmt.Errorf("%w: %s", errors.ErrReservedCmd, args[0])
}
spell.Spell = strings.Join(args, " ")
if spell.Name == "" {
return fmt.Errorf("%w: --name is missing", errors.ErrInsufficientArgs)
}
return app.AddSpell(ctx, spell)
},
}
cmd.Flags().StringVarP(&spell.Name, "name", "n", "", "Spell name")
cmd.Flags().StringSliceVarP(&spell.Tags, "tags", "t", nil, "Spell tags")
cmd.Flags().StringVarP(&spell.ShortDesc, "desc", "d", "", "Short spell description")
return cmd
}
func isReservedCommand(cmd string) bool {
_, reserved := reservedCommands[strings.ToLower(cmd)]
return reserved
}