diff options
Diffstat (limited to 'internal/service')
-rw-r--r-- | internal/service/config.go | 41 | ||||
-rw-r--r-- | internal/service/service.go | 3 |
2 files changed, 21 insertions, 23 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 + } } diff --git a/internal/service/service.go b/internal/service/service.go index 9fbacfb..637a1cf 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -31,7 +31,8 @@ type Service struct { } func NewService(cfg *Config) (*Service, error) { - cfg.Apply(DefaultConfig) + // Apply defaults + cfg.Merge(&DefaultConfig) for _, cmd := range requiredCommands { if err := internal.RequireCommand(cmd); err != nil { |