From e4737ca179b1769baec72406301060fb63d52bc0 Mon Sep 17 00:00:00 2001 From: Kirillkriv Date: Tue, 4 Aug 2026 01:16:32 +0500 Subject: [PATCH] feat - main chore - minor changes --- cmd/main.go | 42 +++++++++++++++++++++++++++++ go.mod | 14 +++++++--- go.sum | 20 ++++++++++++++ internal/adapters/caster/caster.go | 2 +- internal/adapters/sqlite/sqlite.go | 2 +- internal/application/application.go | 10 ++++++- internal/cli/add.go | 2 +- internal/cli/root.go | 3 ++- internal/handlers/handlers.go | 14 ---------- tests/mock/mock.go | 4 +-- 10 files changed, 89 insertions(+), 24 deletions(-) delete mode 100644 internal/handlers/handlers.go diff --git a/cmd/main.go b/cmd/main.go index 06ab7d0..b5f88f5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1 +1,43 @@ package main + +import ( + "context" + "database/sql" + "fmt" + "github.com/abelith/spellbook/internal/adapters/caster" + "github.com/abelith/spellbook/internal/adapters/sqlite" + "github.com/abelith/spellbook/internal/application" + "github.com/abelith/spellbook/internal/cli" + _ "modernc.org/sqlite" + "os" + "os/signal" + "syscall" +) + +func handleErr(err error) { + if err != nil { + _, _ = fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func main() { + ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer cancel() + + db, err := sql.Open("sqlite", "db.sql") + handleErr(err) + + repo := sqlite.NewRepo(db) + handleErr(repo.Init(ctx)) + + app := application.NewApplication(&caster.SpellCaster{}, repo, repo) + + root := cli.NewRootCmd(app) + root.AddCommand( + cli.NewAddCommand(app), + cli.NewRmCommand(app), + ) + + handleErr(root.Execute()) +} diff --git a/go.mod b/go.mod index 70ede52..afb8afc 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module spellbook +module github.com/abelith/spellbook go 1.26.0 @@ -6,17 +6,25 @@ require ( github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/clipperhouse/displaywidth v0.10.0 // indirect github.com/clipperhouse/uax29/v2 v2.6.0 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect github.com/fatih/color v1.18.0 // indirect github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/mattn/go-colorable v0.1.14 // indirect - github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-isatty v0.0.24 // indirect github.com/mattn/go-runewidth v0.0.19 // indirect + github.com/ncruces/go-strftime v1.0.0 // indirect github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect github.com/olekukonko/errors v1.2.0 // indirect github.com/olekukonko/ll v0.1.6 // indirect github.com/olekukonko/tablewriter v1.1.4 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/spf13/cobra v1.10.2 // indirect github.com/spf13/pflag v1.0.9 // indirect - golang.org/x/sys v0.30.0 // indirect + golang.org/x/sys v0.47.0 // indirect + modernc.org/libc v1.74.4 // indirect + modernc.org/mathutil v1.7.1 // indirect + modernc.org/memory v1.11.0 // indirect + modernc.org/sqlite v1.56.0 // indirect ) diff --git a/go.sum b/go.sum index 821803c..a511198 100644 --- a/go.sum +++ b/go.sum @@ -5,18 +5,26 @@ github.com/clipperhouse/displaywidth v0.10.0/go.mod h1:XqJajYsaiEwkxOj4bowCTMcT1 github.com/clipperhouse/uax29/v2 v2.6.0 h1:z0cDbUV+aPASdFb2/ndFnS9ts/WNXgTNNGFoKXuhpos= github.com/clipperhouse/uax29/v2 v2.6.0/go.mod h1:Wn1g7MK6OoeDT0vL+Q0SQLDz/KpfsVRgg6W7ihQeh4g= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM= github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= +github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw= github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs= +github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w= +github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 h1:zrbMGy9YXpIeTnGj4EljqMiZsIcE09mmF8XsD5AYOJc= github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6/go.mod h1:rEKTHC9roVVicUIfZK7DYrdIoM0EOr8mK1Hj5s3JjH0= github.com/olekukonko/errors v1.2.0 h1:10Zcn4GeV59t/EGqJc8fUjtFT/FuUh5bTMzZ1XwmCRo= @@ -25,6 +33,8 @@ github.com/olekukonko/ll v0.1.6 h1:lGVTHO+Qc4Qm+fce/2h2m5y9LvqaW+DCN7xW9hsU3uA= github.com/olekukonko/ll v0.1.6/go.mod h1:NVUmjBb/aCtUpjKk75BhWrOlARz3dqsM+OtszpY4o88= github.com/olekukonko/tablewriter v1.1.4 h1:ORUMI3dXbMnRlRggJX3+q7OzQFDdvgbN9nVWj1drm6I= github.com/olekukonko/tablewriter v1.1.4/go.mod h1:+kedxuyTtgoZLwif3P1Em4hARJs+mVnzKxmsCL/C5RY= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= @@ -34,4 +44,14 @@ go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc= golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +modernc.org/libc v1.74.4 h1:fX1Omw4o2/1C2iRkkIsrQTasJQldLhRmuPreXLoWs9k= +modernc.org/libc v1.74.4/go.mod h1:eeQAS9W3sZeKYMFubydxJpII9ybHWshk+7or7bLG9co= +modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU= +modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg= +modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI= +modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= +modernc.org/sqlite v1.56.0 h1:/D8e2RfFqoy/Zc6PuC76U28zFwmI/sYx1Kjm4yEn9e0= +modernc.org/sqlite v1.56.0/go.mod h1:yCJ2cmAaIkHQ25oXWrF8H4O1lIfPYPR26yCEDj2P3pQ= diff --git a/internal/adapters/caster/caster.go b/internal/adapters/caster/caster.go index 03862e8..5d53c17 100644 --- a/internal/adapters/caster/caster.go +++ b/internal/adapters/caster/caster.go @@ -5,9 +5,9 @@ import ( "context" "errors" "fmt" + "github.com/abelith/spellbook/internal/models" "github.com/google/shlex" "os/exec" - "spellbook/internal/models" "text/template" "time" ) diff --git a/internal/adapters/sqlite/sqlite.go b/internal/adapters/sqlite/sqlite.go index 46103dd..c89fe7d 100644 --- a/internal/adapters/sqlite/sqlite.go +++ b/internal/adapters/sqlite/sqlite.go @@ -4,7 +4,7 @@ import ( "context" "database/sql" "fmt" - "spellbook/internal/models" + "github.com/abelith/spellbook/internal/models" "strings" ) diff --git a/internal/application/application.go b/internal/application/application.go index 3276ea7..bd6fbea 100644 --- a/internal/application/application.go +++ b/internal/application/application.go @@ -2,7 +2,7 @@ package application import ( "context" - "spellbook/internal/models" + "github.com/abelith/spellbook/internal/models" ) type SpellCaster interface { @@ -25,6 +25,14 @@ type Application struct { spellQuery SpellQuery } +func NewApplication(caster SpellCaster, cmd SpellCommand, query SpellQuery) *Application { + return &Application{ + caster: caster, + spellCmd: cmd, + spellQuery: query, + } +} + func (a *Application) AddSpell( ctx context.Context, spell models.Spell, diff --git a/internal/cli/add.go b/internal/cli/add.go index d66cf84..dafe52b 100644 --- a/internal/cli/add.go +++ b/internal/cli/add.go @@ -1,8 +1,8 @@ package cli import ( + "github.com/abelith/spellbook/internal/models" "github.com/spf13/cobra" - "spellbook/internal/models" "strings" ) diff --git a/internal/cli/root.go b/internal/cli/root.go index bc4f16b..05ffa87 100644 --- a/internal/cli/root.go +++ b/internal/cli/root.go @@ -2,10 +2,10 @@ package cli import ( "context" + "github.com/abelith/spellbook/internal/models" "github.com/olekukonko/tablewriter" "github.com/spf13/cobra" "os" - "spellbook/internal/models" "strings" ) @@ -63,6 +63,7 @@ func listCommands(ctx context.Context, cmd *cobra.Command, app Application) erro if len(spells) == 0 { cmd.Println("Spell book is empty. Try `spellbook add` to add your first spell") + return nil } return printTable(cmd, spells) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go deleted file mode 100644 index 1ec4eb5..0000000 --- a/internal/handlers/handlers.go +++ /dev/null @@ -1,14 +0,0 @@ -package handlers - -import ( - "context" - "spellbook/internal/models" -) - -type Application interface { - AddSpell(ctx context.Context, spell models.Spell) error - GetSpell(ctx context.Context, spellName string) (models.Spell, error) - GetSpells(ctx context.Context, query models.SearchQuery) ([]models.Spell, error) - DeleteSpell(ctx context.Context, spellName string) error - CastSpell(ctx context.Context, spellCast models.SpellCast) (models.CastResult, error) -} diff --git a/tests/mock/mock.go b/tests/mock/mock.go index 684545e..d62a6af 100644 --- a/tests/mock/mock.go +++ b/tests/mock/mock.go @@ -3,8 +3,8 @@ package mock import ( "context" "fmt" - "spellbook/internal/adapters/caster" - "spellbook/internal/models" + "github.com/abelith/spellbook/internal/adapters/caster" + "github.com/abelith/spellbook/internal/models" ) var testSpell = models.Spell{