chore: cli refactor
This commit is contained in:
1 parent
181642172a
commit
99264ca438
8 files changed
+27
-50
No files matched your search
+59
@@ -0,0 +1,59 @@
|
||||
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
|
||||
}
|
||||
Reference in new issue
Block a user