Skip to content

Commit

Permalink
feat: do no exit with error when config file is not set
Browse files Browse the repository at this point in the history
If the default config file (config.yml) doesn't exist, don't exit with
an error. Instead, use a default configuration file. This removes a
small point of friction when gettting started with go-librespot.

On the other hand, if the config path is set explicitly using
`-config_path`, do exit with an error when it cannot be found (because
presumably the user made a mistake there).
  • Loading branch information
aykevl authored and devgianlu committed Sep 10, 2024
1 parent 54a2210 commit e4fb319
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions cmd/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,13 +354,19 @@ type Config struct {
}

func loadConfig(cfg *Config) error {
flag.StringVar(&cfg.ConfigPath, "config_path", "config.yml", "the configuration file path")
flag.StringVar(&cfg.ConfigPath, "config_path", "", "the configuration file path (default config.yml)")
flag.StringVar(&cfg.CredentialsPath, "credentials_path", "credentials.json", "the credentials file path")
flag.Parse()

configBytes, err := os.ReadFile(cfg.ConfigPath)
configPath := cfg.ConfigPath
if configPath == "" {
configPath = "config.yml"
}
configBytes, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("failed reading configuration file: %w", err)
if cfg.ConfigPath != "" || !os.IsNotExist(err) {
return fmt.Errorf("failed reading configuration file: %w", err)
}
}

if err := yaml.Unmarshal(configBytes, cfg); err != nil {
Expand Down

0 comments on commit e4fb319

Please sign in to comment.