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,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"github.com/abelith/spellbook/cmd"
|
||||
"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/config"
|
||||
_ "modernc.org/sqlite"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// Version is set on build via -ldflags
|
||||
var Version = "dev"
|
||||
|
||||
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()
|
||||
|
||||
cfg, err := config.NewConfig()
|
||||
handleErr(err)
|
||||
|
||||
dir := filepath.Dir(cfg.DBPath)
|
||||
handleErr(os.MkdirAll(dir, 0755))
|
||||
|
||||
db, err := sql.Open("sqlite", cfg.DBPath)
|
||||
handleErr(err)
|
||||
|
||||
repo := sqlite.NewRepo(db)
|
||||
handleErr(repo.Init(ctx))
|
||||
|
||||
app := application.NewApplication(&caster.SpellCaster{}, repo, repo)
|
||||
|
||||
root := cmd.NewRootCmd(Version, app)
|
||||
root.AddCommand(
|
||||
cmd.NewAddCommand(app),
|
||||
cmd.NewRmCommand(app),
|
||||
cmd.NewSearchCmd(app),
|
||||
cmd.NewExportCmd(app),
|
||||
cmd.NewImportCmd(app),
|
||||
)
|
||||
|
||||
if err := root.Execute(); err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user