diff --git a/CHANGELOG.md b/CHANGELOG.md index a445957..fd0d863 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## [Latest] +## [0.2.0-alpha] - 2026-08-08 + +### Added +- Configuration options + +## [Unreleased] + ## [0.1.0-alpha] - 2026-08-04 ### Added diff --git a/README.md b/README.md index c984c44..4f86451 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ ## Table of contents - [Features](#features) - [Building from Source](#building-from-source) +- [Configuration](#configuration) - [Usage](#usage) - [Upcoming Features](#upcoming-features) @@ -34,10 +35,20 @@ cd spellbook # Build static binary CGO_ENABLED=0 go build -o spellbook . + +# Move to bin directory (for Linux/macOS) +mv spellbook $(go env GOPATH)/bin/ ``` --- +## Configuration + +### Env +- `SPELLBOOK_DB_PATH`: db location (example: `/spellbook/spellbook.db`) + +--- + ## Usage - Use `spellbook help` to list commands @@ -95,5 +106,4 @@ spellbook my-spell target=127.0.0.1 --- ## Upcoming Features -- Search to navigate your spell book -- Configuration \ No newline at end of file +- Search to navigate your spell book \ No newline at end of file diff --git a/cmd/main.go b/cmd/main.go index a622ab5..cfbabb1 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -7,10 +7,12 @@ import ( "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" "github.com/abelith/spellbook/internal/controllers/cli" _ "modernc.org/sqlite" "os" "os/signal" + "path/filepath" "syscall" ) @@ -25,7 +27,13 @@ func main() { ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer cancel() - db, err := sql.Open("sqlite", "db.sql") + 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) diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..6513755 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,32 @@ +package config + +import ( + "os" + "path/filepath" +) + +type Config struct { + DBPath string +} + +func NewConfig() (*Config, error) { + dsn, err := getDBPath() + if err != nil { + return nil, err + } + + return &Config{DBPath: dsn}, nil +} + +func getDBPath() (string, error) { + if dsn := os.Getenv("SPELLBOOK_DB_PATH"); dsn != "" { + return dsn, nil + } + + dir, err := os.UserConfigDir() + if err != nil { + return "", err + } + + return filepath.Join(dir, "spellbook", "spellbook.db"), nil +}