@@ -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
370370type 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+
428438func 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