Skip to content

Commit

Permalink
feat: use XDG specification (#23)
Browse files Browse the repository at this point in the history
* feat: default database to the XDG data dir

* docs(readme): update data section
  • Loading branch information
Broderick-Westrope authored Nov 15, 2024
1 parent c270928 commit 28565ce
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ An example configuration file is provided in [`example.config.toml`](./example.c

## Data

The game data is stored in a SQLite database. By default, the database is stored in the working directory as `tetrigo.db`. You can specify a different file using the `--db` flag.
The game data is stored in a SQLite database. By default, the database is stored in `./tetrigo/tetrigo.db` within the devices XDG data (or equivalent) directory. The [adrg/xdg](https://github.com/adrg/xdg) defines values `XDG_DATA_DIRS` for various operating systems (eg. on macOS if the `/Library/Application Support` directory exists it will be stored there, otherwise in `~/.local/share`). You can specify a different file path using the `--db` flag.

```bash
./tetrigo --db=/path/to/data.db
Expand Down
14 changes: 13 additions & 1 deletion cmd/tetrigo/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import (
"log"

"github.com/adrg/xdg"
"github.com/alecthomas/kong"
)

Expand All @@ -14,7 +17,7 @@ type CLI struct {

type GlobalVars struct {
Config string `help:"Path to config file" default:"config.toml" type:"path"`
DB string `help:"Path to database file" default:"tetrigo.db"`
DB string `help:"Path to database file. Empty value will use XDG data directory." default:""`
}

func main() {
Expand All @@ -24,6 +27,15 @@ func main() {
kong.Description("A tetris TUI written in Go"),
kong.UsageOnError(),
)

if cli.GlobalVars.DB == "" {
var err error
cli.GlobalVars.DB, err = xdg.DataFile("./tetrigo/tetrigo.db")
if err != nil {
log.Fatal(err)
}
}

// Call the Run() method of the selected parsed command.
err := ctx.Run(&cli.GlobalVars)
ctx.FatalIfErrorf(err)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/BurntSushi/toml v1.3.2
github.com/adrg/xdg v0.5.3
github.com/alecthomas/kong v0.8.1
github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.25.0
Expand All @@ -29,7 +30,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.3.8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/adrg/xdg v0.5.3 h1:xRnxJXne7+oWDatRhR1JLnvuccuIeCoBu2rtuLqQB78=
github.com/adrg/xdg v0.5.3/go.mod h1:nlTsY+NNiCBGCK2tpm09vRqfVzrc2fLmXGpBLF0zlTQ=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/assert/v2 v2.1.0/go.mod h1:b/+1DI2Q6NckYi+3mXyH3wFb8qG37K/DuK80n7WefXA=
github.com/alecthomas/kong v0.8.1 h1:acZdn3m4lLRobeh3Zi2S2EpnXTd1mOL6U7xVml+vfkY=
Expand Down Expand Up @@ -55,8 +57,8 @@ golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
Expand Down

0 comments on commit 28565ce

Please sign in to comment.