diff options
Diffstat (limited to 'internal/service/config.go')
-rw-r--r-- | internal/service/config.go | 41 |
1 files changed, 24 insertions, 17 deletions
diff --git a/internal/service/config.go b/internal/service/config.go index bcb9efc..8105cd5 100644 --- a/internal/service/config.go +++ b/internal/service/config.go @@ -1,30 +1,25 @@ package service import ( - "encoding/json" "fmt" "os" "time" "dario.cat/mergo" + "git.server.ky/slackcoder/mirror/internal" + "github.com/BurntSushi/toml" ) type Duration struct { time.Duration } -func (s Duration) MarshalJSON() ([]byte, error) { - return json.Marshal(s.Duration.String()) +func (s Duration) MarshalText() ([]byte, error) { + return []byte(s.Duration.String()), nil } -func (s *Duration) UnmarshalJSON(data []byte) error { - var str string - err := json.Unmarshal(data, &str) - if err != nil { - return err - } - - v, err := time.ParseDuration(str) +func (s *Duration) UnmarshalText(text []byte) error { + v, err := time.ParseDuration(string(text)) if err != nil { return err } @@ -33,15 +28,26 @@ func (s *Duration) UnmarshalJSON(data []byte) error { return nil } +// Global parameters +type GlobalConfig struct { + MaxInterval Duration `toml:"max-interval"` + MinInterval Duration `toml:"min-interval"` +} + type Config struct { - MaxInterval Duration `json:"max-interval,omitempty"` - MinInterval Duration `json:"min-interval,omitempty"` - Mirrors []*Mirror `json:"mirrors,omitempty"` + *GlobalConfig `toml:"global"` + Mirrors []*Mirror `toml:"mirrors,omitempty"` +} + +func (c *Config) String() string { + return internal.MustTOML(c) } var DefaultConfig = Config{ - MaxInterval: Duration{24 * time.Hour}, - MinInterval: Duration{time.Hour}, + GlobalConfig: &GlobalConfig{ + MaxInterval: Duration{24 * time.Hour}, + MinInterval: Duration{time.Hour}, + }, } func (c *Config) Apply(arg Config) { @@ -63,11 +69,12 @@ func ApplyFileConfig(cfg *Config, filePath string) error { } defer f.Close() - err = json.NewDecoder(f).Decode(&ret) + _, err = toml.NewDecoder(f).Decode(&ret) if err != nil { return fmt.Errorf("loading configuration file: %w", err) } cfg.Apply(ret) + return nil } |