1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package service
import (
"fmt"
"os"
"time"
"git.server.ky/slackcoder/mirror/internal"
"github.com/BurntSushi/toml"
)
type Duration struct {
time.Duration
}
func (s Duration) MarshalText() ([]byte, error) {
return []byte(s.Duration.String()), nil
}
func (s *Duration) UnmarshalText(text []byte) error {
v, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*s = Duration{v}
return nil
}
// Global parameters
type GlobalConfig struct {
MaxInterval Duration `toml:"max-interval"`
MinInterval Duration `toml:"min-interval"`
}
type Config struct {
GlobalConfig `toml:"global"`
Mirrors []*Mirror `toml:"mirrors,omitempty"`
}
func (c *Config) String() string {
return internal.MustTOML(c)
}
var DefaultConfig = Config{
GlobalConfig: GlobalConfig{
MaxInterval: Duration{24 * time.Hour},
MinInterval: Duration{time.Hour},
},
}
func ReadConfig(fp string) (*Config, error) {
var config Config
f, err := os.Open(fp)
if os.IsNotExist(err) {
return nil, nil
} else if err != nil {
return nil, err
}
defer f.Close()
_, err = toml.NewDecoder(f).Decode(&config)
if err != nil {
return nil, fmt.Errorf("loading configuration file: %w", err)
}
return &config, 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
}
}
|