diff options
author | Slack Coder <slackcoder@server.ky> | 2024-10-16 17:53:47 -0500 |
---|---|---|
committer | Slack Coder <slackcoder@server.ky> | 2024-10-16 17:53:47 -0500 |
commit | fecc42849871ec040aac004bcf80ff85e97d4d87 (patch) | |
tree | e0d04fe8ac282f17d8f2504b66eb6002fe9c4cf5 /internal/service/config.go | |
parent | ab795d1030504671668a2ca96a74d195363c7108 (diff) | |
download | mirror-fecc42849871ec040aac004bcf80ff85e97d4d87.tar.xz |
Make config handling simpler
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 + } } |