diff options
-rw-r--r-- | cmd/mirror/main.go | 5 | ||||
-rw-r--r-- | internal/service/config.go | 41 | ||||
-rw-r--r-- | internal/service/service.go | 3 |
3 files changed, 23 insertions, 26 deletions
diff --git a/cmd/mirror/main.go b/cmd/mirror/main.go index 4d2e712..8276a10 100644 --- a/cmd/mirror/main.go +++ b/cmd/mirror/main.go @@ -32,11 +32,10 @@ func exitOnError(err error) { func main() { flags := ParseFlags() - var cfg service.Config - err := service.ApplyFileConfig(&cfg, flags.Config) + cfg, err := service.ReadConfig(flags.Config) exitOnError(err) - srv, err := service.NewService(&cfg) + srv, err := service.NewService(cfg) exitOnError(err) err = srv.Run() 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 { |