diff options
Diffstat (limited to 'internal/service/config.go')
-rw-r--r-- | internal/service/config.go | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/internal/service/config.go b/internal/service/config.go index 0c42755..27368b3 100644 --- a/internal/service/config.go +++ b/internal/service/config.go @@ -49,36 +49,33 @@ var DefaultConfig = Config{ }, } -func (c *Config) Apply(src Config) { - if c.MaxInterval.Duration == 0 && src.MaxInterval.Duration != 0 { - c.MaxInterval = src.MaxInterval - } - if c.MinInterval.Duration == 0 && src.MinInterval.Duration != 0 { - c.MinInterval = src.MinInterval - } - if len(c.Mirrors) == 0 { - c.Mirrors = src.Mirrors - } -} - -// ApplyFileConfig loads the configuration described by the given yaml file. -func ApplyFileConfig(cfg *Config, filePath string) error { - var ret Config +func ReadConfig(fp string) (*Config, error) { + var config Config - f, err := os.Open(filePath) + f, err := os.Open(fp) if os.IsNotExist(err) { - return nil + return nil, nil } else if err != nil { - return err + return nil, err } defer f.Close() - _, err = toml.NewDecoder(f).Decode(&ret) + _, err = toml.NewDecoder(f).Decode(&config) if err != nil { - return fmt.Errorf("loading configuration file: %w", err) + return nil, fmt.Errorf("loading configuration file: %w", err) } - cfg.Apply(ret) + return &config, nil +} - return nil +func (c *Config) Merge(src *Config) { + if c.MaxInterval.Duration == 0 && src.MaxInterval.Duration != 0 { + c.MaxInterval = src.MaxInterval + } + if c.MinInterval.Duration == 0 && src.MinInterval.Duration != 0 { + c.MinInterval = src.MinInterval + } + if len(c.Mirrors) == 0 { + c.Mirrors = src.Mirrors + } } |