Skip to content

Commit 5c73c70

Browse files
committed
add --cache flag
Default state dir moves from os.UserConfigDir to os.UserCacheDir
1 parent 8061f62 commit 5c73c70

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

README.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,18 @@ Details about cross-compiling go-librespot are described [here](/CROSS_COMPILE.m
6060

6161
## Configuration
6262

63-
The default directory for configuration files is `~/.config/go-librespot`. On macOS devices, this is
64-
`~/Library/Application Support/go-librespot`. You can change this directory with the
65-
`--config_dir` flag. The configuration directory contains:
63+
The main configuration files is `~/.config/go-librespot/config.yaml`. On macOS devices, this is
64+
`~/Library/Application Support/go-librespot/config.yaml`. It does not exist by default.
6665

67-
- `config.yml`: The main configuration (does not exist by default)
68-
- `state.json`: The player state and credentials
69-
- `lockfile`: A lockfile to prevent running multiple instances on the same configuration
66+
You can change this path with the `--config` flag.
67+
68+
Cache files are stored in `~/.cache/go-librespot` (`~/Library/Caches/go-librespot` on macOS).
7069

71-
You may also specify a custom config file with the `--config` flag.
70+
You can change this directory with the `--cache` flag.
71+
72+
The cache directory contains:
73+
- `state.json`: The player state and credentials
74+
- `lockfile`: A lockfile to prevent multiple instances using the same state
7275

7376
The full configuration schema is available [here](/config_schema.json), only the main options are detailed below.
7477

cmd/daemon/main.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func NewApp(cfg *Config) (app *App, err error) {
7979
}
8080

8181
app.state.SetLogger(app.log)
82-
if err := app.state.Read(cfg.ConfigDir); err != nil {
82+
if err := app.state.Read(cfg.CacheDir); err != nil {
8383
return nil, err
8484
}
8585

@@ -368,7 +368,7 @@ func (app *App) withAppPlayer(ctx context.Context, appPlayerFunc func(context.Co
368368
}
369369

370370
type Config struct {
371-
ConfigDir string `koanf:"config_dir"`
371+
CacheDir string `koanf:"cache"`
372372
ConfigPath string `koanf:"config"`
373373

374374
// We need to keep this object around, otherwise it gets GC'd and the
@@ -425,8 +425,19 @@ type Config struct {
425425
} `koanf:"credentials"`
426426
}
427427

428+
// backwards compatibility for config_dir flag
429+
func aliasNormalizeFunc(f *flag.FlagSet, name string) flag.NormalizedName {
430+
switch name {
431+
case "config_dir":
432+
name = "cache"
433+
break
434+
}
435+
return flag.NormalizedName(name)
436+
}
437+
428438
func loadConfig(cfg *Config) error {
429439
f := flag.NewFlagSet("config", flag.ContinueOnError)
440+
f.SetNormalizeFunc(aliasNormalizeFunc)
430441
f.Usage = func() {
431442
fmt.Println(f.FlagUsages())
432443
os.Exit(0)
@@ -435,26 +446,30 @@ func loadConfig(cfg *Config) error {
435446
if err != nil {
436447
return err
437448
}
438-
defaultConfigDir := filepath.Join(userConfigDir, "go-librespot")
439-
f.StringVar(&cfg.ConfigDir, "config_dir", defaultConfigDir, "the configuration directory")
440-
441-
defaultConfigPath := filepath.Join(defaultConfigDir, "config.yaml")
449+
defaultConfigPath := filepath.Join(userConfigDir, "go-librespot", "config.yaml")
442450
f.StringVar(&cfg.ConfigPath, "config", defaultConfigPath, "the configuration file")
443451

452+
userCacheDir, err := os.UserCacheDir()
453+
if err != nil {
454+
return err
455+
}
456+
defaultCachePath := filepath.Join(userCacheDir, "go-librespot")
457+
f.StringVar(&cfg.CacheDir, "cache", defaultCachePath, "the cache directory")
458+
444459
err = f.Parse(os.Args[1:])
445460
if err != nil {
446461
return err
447462
}
448463

449464
// Make config directory if needed.
450-
err = os.MkdirAll(cfg.ConfigDir, 0o700)
465+
err = os.MkdirAll(cfg.CacheDir, 0o700)
451466
if err != nil {
452467
return fmt.Errorf("failed creating config directory: %w", err)
453468
}
454469

455470
// Lock the config directory (to ensure multiple instances won't clobber
456471
// each others state).
457-
lockFilePath := filepath.Join(cfg.ConfigDir, "lockfile")
472+
lockFilePath := filepath.Join(cfg.CacheDir, "lockfile")
458473
cfg.configLock = flock.New(lockFilePath)
459474
if locked, err := cfg.configLock.TryLock(); err != nil {
460475
return fmt.Errorf("could not lock config directory: %w", err)
@@ -487,7 +502,8 @@ func loadConfig(cfg *Config) error {
487502
// load file configuration (if available)
488503
var configPath string
489504
if _, err := os.Stat(cfg.ConfigPath); os.IsNotExist(err) {
490-
configPath = filepath.Join(cfg.ConfigDir, "config.yml")
505+
// postel: allow .yml in place of .yaml
506+
configPath = strings.TrimSuffix(cfg.ConfigPath, filepath.Ext(cfg.ConfigPath)) + ".yml"
491507
} else {
492508
configPath = cfg.ConfigPath
493509
}

0 commit comments

Comments
 (0)