Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use XDG for config #24

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ To see more options for starting the game you can run:

More complex configuration can be done using a TOML file. If no config file is found sensible defaults will be used.

By default, Tetrigo will look for the file `config.toml` in the working directory. You can specify a different file using the `--config` flag.
By default, Tetrigo will look for the file `./tetrigo/config.toml` within the devices XDG config (or equivalent) directory. The [adrg/xdg](https://github.com/adrg/xdg) defines values `XDG_CONFIG_HOME` for various operating systems (eg. on macOS it is `~/Library/Application Support` directory exists it will be stored there, otherwise in `~/Library/Preferences`). You can specify a different file using the `--config` flag.

```bash
./tetrigo --config=/path/to/config.toml
Expand All @@ -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 `./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.
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 `XDG_DATA_HOME` for various operating systems (eg. on macOS if the `~/Library/Application Support` directory exists it will be stored there, otherwise in `/Library/Application Support`). You can specify a different file path using the `--db` flag.

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

import (
"log"

"github.com/adrg/xdg"
"github.com/alecthomas/kong"
)
Expand All @@ -16,7 +14,7 @@ type CLI struct {
}

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

Expand All @@ -28,15 +26,29 @@ func main() {
kong.UsageOnError(),
)

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

// Call the Run() method of the selected parsed command.
err := ctx.Run(&cli.GlobalVars)
ctx.FatalIfErrorf(err)
}

func handleDefaultGlobals(g *GlobalVars) error {
if g.Config == "" {
var err error
g.Config, err = xdg.ConfigFile("./tetrigo/config.toml")
if err != nil {
return err
}
}
if g.DB == "" {
var err error
g.DB, err = xdg.DataFile("./tetrigo/tetrigo.db")
if err != nil {
return err
}
}
return nil
}
8 changes: 4 additions & 4 deletions cmd/tetrigo/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ func (c *LeaderboardCmd) Run(globals *GlobalVars) error {
func launchStarter(globals *GlobalVars, starterMode tui.Mode, switchIn tui.SwitchModeInput) error {
db, err := data.NewDB(globals.DB)
if err != nil {
return fmt.Errorf("error opening database: %w", err)
return fmt.Errorf("opening database: %w", err)
}

cfg, err := config.GetConfig(globals.Config)
if err != nil {
return fmt.Errorf("error getting config: %w", err)
return fmt.Errorf("getting config: %w", err)
}

model, err := starter.NewModel(
starter.NewInput(starterMode, switchIn, db, cfg),
)
if err != nil {
return fmt.Errorf("error creating starter model: %w", err)
return fmt.Errorf("creating starter model: %w", err)
}

if _, err = tea.NewProgram(model, tea.WithAltScreen()).Run(); err != nil {
return fmt.Errorf("error running tea program: %w", err)
return fmt.Errorf("running tea program: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ func GetConfig(path string) (*Config, error) {
if errors.Is(err, os.ErrNotExist) {
return &c, nil
}
return nil, err
return nil, fmt.Errorf("decoding toml file: %w", err)
}

err = c.validate()
if err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
return nil, fmt.Errorf("validating config: %w", err)
}

return &c, nil
Expand Down
Loading