From 9434b02422e2b64caa8d6a8706b68c78299bba95 Mon Sep 17 00:00:00 2001 From: Kirillkriv Date: Fri, 14 Aug 2026 19:14:18 +0500 Subject: [PATCH] fix - check CHANGELOG.md ver 0.2.1-alpha --- CHANGELOG.md | 8 ++++++++ README.md | 5 +++-- cmd/main.go | 4 +++- internal/adapters/caster/caster.go | 3 ++- internal/adapters/sqlite/sqlite.go | 4 ++-- internal/controllers/cli/add.go | 22 ++++++++++++++++++++++ internal/controllers/cli/rm.go | 10 +++++++++- internal/errors/errors.go | 10 ++++++++++ 8 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 internal/errors/errors.go diff --git a/CHANGELOG.md b/CHANGELOG.md index fd0d863..fa7f826 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## [Latest] +## [0.2.1-alpha]- 2026-8-14 + +### Fixed +- Prevented creating spells with reserved names +- Improved error messages + +## [Unreleased] + ## [0.2.0-alpha] - 2026-08-08 ### Added diff --git a/README.md b/README.md index 4f86451..5dca74c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# spellbook 0.1.0-alpha +# spellbook 0.2.1-alpha `spellbook` is a lightweight CLI command manager written in Go. It allows you to save complex shell commands as parameterized templates ("spells") and execute them instantly with custom arguments. @@ -106,4 +106,5 @@ spellbook my-spell target=127.0.0.1 --- ## Upcoming Features -- Search to navigate your spell book \ No newline at end of file +- Search to navigate your spell book +- Export/import spellbooks \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index cfbabb1..98ceec9 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -43,5 +43,7 @@ func main() { c := cli.NewCLI(app) - handleErr(c.Run(ctx)) + if err := c.Run(ctx); err != nil { + os.Exit(1) + } } diff --git a/internal/adapters/caster/caster.go b/internal/adapters/caster/caster.go index 5d53c17..f1a35c0 100644 --- a/internal/adapters/caster/caster.go +++ b/internal/adapters/caster/caster.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + errors2 "github.com/abelith/spellbook/internal/errors" "github.com/abelith/spellbook/internal/models" "github.com/google/shlex" "os/exec" @@ -40,7 +41,7 @@ func (sc *SpellCaster) Cast(ctx context.Context, cast models.SpellCast) (models. res.Err = err return res, nil } - return res, fmt.Errorf("failed to cast spell: %w", err) + return res, fmt.Errorf("%w: %w", errors2.ErrFailedToCast, err) } res.ExitCode = 0 diff --git a/internal/adapters/sqlite/sqlite.go b/internal/adapters/sqlite/sqlite.go index c89fe7d..95defd7 100644 --- a/internal/adapters/sqlite/sqlite.go +++ b/internal/adapters/sqlite/sqlite.go @@ -3,7 +3,7 @@ package sqlite import ( "context" "database/sql" - "fmt" + "github.com/abelith/spellbook/internal/errors" "github.com/abelith/spellbook/internal/models" "strings" ) @@ -33,7 +33,7 @@ func NewRepo(db *sql.DB) *Repo { func (r *Repo) Init(ctx context.Context) error { if r.db == nil { - return fmt.Errorf("no db provided") + return errors.ErrNoDBProvided } if _, err := r.db.ExecContext(ctx, schema); err != nil { diff --git a/internal/controllers/cli/add.go b/internal/controllers/cli/add.go index dafe52b..96ed653 100644 --- a/internal/controllers/cli/add.go +++ b/internal/controllers/cli/add.go @@ -1,11 +1,20 @@ package cli 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": {}, +} + func NewAddCommand(app Application) *cobra.Command { spell := models.Spell{} @@ -16,6 +25,14 @@ func NewAddCommand(app Application) *cobra.Command { 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, " ") return app.AddSpell(ctx, spell) }, @@ -27,3 +44,8 @@ func NewAddCommand(app Application) *cobra.Command { return cmd } + +func isReservedCommand(cmd string) bool { + _, reserved := reservedCommands[strings.ToLower(cmd)] + return reserved +} diff --git a/internal/controllers/cli/rm.go b/internal/controllers/cli/rm.go index 276554a..5f6d61a 100644 --- a/internal/controllers/cli/rm.go +++ b/internal/controllers/cli/rm.go @@ -1,6 +1,10 @@ package cli -import "github.com/spf13/cobra" +import ( + "fmt" + "github.com/abelith/spellbook/internal/errors" + "github.com/spf13/cobra" +) func NewRmCommand(app Application) *cobra.Command { cmd := &cobra.Command{ @@ -10,6 +14,10 @@ func NewRmCommand(app Application) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() + if len(args) < 1 { + return fmt.Errorf("%w: %s", errors.ErrInsufficientArgs, "spell_name") + } + return app.DeleteSpell(ctx, args[0]) }, } diff --git a/internal/errors/errors.go b/internal/errors/errors.go new file mode 100644 index 0000000..ad1c26a --- /dev/null +++ b/internal/errors/errors.go @@ -0,0 +1,10 @@ +package errors + +import "fmt" + +var ( + ErrReservedCmd = fmt.Errorf("command name is reserved") + ErrInsufficientArgs = fmt.Errorf("mandatory args are missing") + ErrFailedToCast = fmt.Errorf("failed to cast spell") + ErrNoDBProvided = fmt.Errorf("no db provided") +)