From 55383869d1f9b03511053e0e611f2d92c7381785 Mon Sep 17 00:00:00 2001 From: grumbulon Date: Mon, 9 Jan 2023 17:25:40 -0500 Subject: [PATCH] add check for xdg config path and cwd directory config.yaml -- xdg takes presidence if both exist --- .gitignore | 3 ++- internal/configuration.go | 29 ++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index eda73ad..d46b0b2 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,5 @@ go.work pomme test.db test.sqlite -.dccache \ No newline at end of file +.dccache +config.yaml \ No newline at end of file diff --git a/internal/configuration.go b/internal/configuration.go index 15a44df..254d9af 100644 --- a/internal/configuration.go +++ b/internal/configuration.go @@ -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) + }