add check for xdg config path and cwd directory config.yaml -- xdg takes presidence if both exist

This commit is contained in:
grumbulon 2023-01-09 17:25:40 -05:00
parent a1f848d423
commit 55383869d1
2 changed files with 22 additions and 10 deletions

3
.gitignore vendored
View File

@ -24,4 +24,5 @@ go.work
pomme
test.db
test.sqlite
.dccache
.dccache
config.yaml

View File

@ -49,19 +49,30 @@ type Config struct {
var config Config
func ReadConfig() (*Config, error) {
configPath := xdg.ConfigHome + "/pomme/config.yaml"
var (
data []byte
err error
defaultConfigPath string
)
if configPath != "" {
data, err := os.ReadFile(filepath.Clean(configPath))
if err != nil {
return &Config{}, fmt.Errorf("unable to read config file: %w", err)
}
defaultConfigPath = xdg.ConfigHome + "/pomme/config.yaml"
if data, err = os.ReadFile(filepath.Clean(defaultConfigPath)); err == nil {
if err = yaml.Unmarshal(data, &config); err != nil {
err = yaml.Unmarshal(data, &config)
if err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
}
return &config, nil
}
return &config, nil
if data, err = os.ReadFile(filepath.Clean("./config.yaml")); err == nil {
if err = yaml.Unmarshal(data, &config); err != nil {
return &Config{}, fmt.Errorf("unable to unmarshal config file: %w", err)
}
return &config, nil
}
return &Config{}, fmt.Errorf("unable to read config file: %w", err)
}