chore: cli refactor
This commit is contained in:
1 parent
181642172a
commit
99264ca438
8 files changed
+27
-50
No files matched your search
@@ -0,0 +1,47 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/abelith/spellbook/internal/errors"
|
||||
"github.com/abelith/spellbook/internal/models"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func NewSearchCmd(app Application) *cobra.Command {
|
||||
query := models.SearchQuery{}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "search [query]",
|
||||
Short: "Search for spells",
|
||||
Long: "WIP",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := cmd.Context()
|
||||
|
||||
if query.IsEmpty() {
|
||||
return errors.ErrEmptyQuery
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
query.Text = args[0]
|
||||
}
|
||||
|
||||
spells, err := app.GetSpells(ctx, query)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(spells) < 1 {
|
||||
return fmt.Errorf("%w: spells", errors.ErrNotFound)
|
||||
}
|
||||
|
||||
return printTable(cmd, spells)
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().StringVarP(&query.Name, "name", "n", "", "Spell name")
|
||||
cmd.Flags().StringVarP(&query.Command, "cmd", "c", "", "Spell command template")
|
||||
cmd.Flags().StringVarP(&query.Description, "desc", "d", "", "Spell description")
|
||||
cmd.Flags().StringSliceVarP(&query.Tags, "tags", "t", nil, "Spell tags")
|
||||
|
||||
return cmd
|
||||
}
|
||||
Reference in new issue
Block a user