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) } }