Files
abelith 4447b7343f feat: search cmd
chore: raw sql replaced with squirrel
fix: -n is now required for add cmd
2026-08-15 18:38:26 +05:00

98 lines
2.2 KiB
Go

package cli
import (
"context"
"github.com/abelith/spellbook/internal/models"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"os"
"strings"
)
func rootCmd(app Application) func(*cobra.Command, []string) error {
return func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
if len(args) < 1 {
return listCommands(ctx, cmd, app)
}
return runCommand(ctx, app, args)
}
}
func runCommand(ctx context.Context, app Application, args []string) error {
spell, err := app.GetSpell(ctx, args[0])
if err != nil {
return err
}
m := make(map[string]string, len(args[1:]))
for _, pair := range args[1:] {
keyVal := strings.Split(pair, "=")
m[keyVal[0]] = keyVal[1]
}
_, err = app.CastSpell(ctx, models.SpellCast{
Spell: spell,
Args: m,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
})
if err != nil {
return err
}
return nil
}
func listCommands(ctx context.Context, cmd *cobra.Command, app Application) error {
spells, err := app.GetSpells(ctx, models.SearchQuery{})
if err != nil {
return err
}
if len(spells) == 0 {
cmd.Println("Spell book is empty. Try `spellbook add` to add your first spell")
return nil
}
return printTable(cmd, spells)
}
func printTable(cmd *cobra.Command, spells []models.Spell) error {
table := tablewriter.NewTable(cmd.OutOrStdout())
rows := make([][]string, 0, len(spells))
for _, spell := range spells {
row := []string{
spell.Name, spell.Spell, spell.ShortDesc, strings.Join(spell.Tags, ", "),
}
rows = append(rows, row)
}
table.Header("Name", "Spell", "Short description", "Tags")
_ = table.Bulk(rows)
if err := table.Render(); err != nil {
return err
}
return nil
}
func NewRootCmd(ver string, app Application) *cobra.Command {
cmd := &cobra.Command{
Use: "spellbook",
Short: "CLI tool to store, manage and execute command templates",
Long: `Spellbook is a lightweight binary command manager written in Go.
It allows you to save complex shell commands as parametrized templates (spells)
and execute them instantly with custom arguments from anywhere in your terminal`,
RunE: rootCmd(app),
Args: cobra.ArbitraryArgs,
Version: ver,
}
return cmd
}