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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
package service
import (
"fmt"
"os"
"path/filepath"
"time"
"git.server.ky/slackcoder/mirror/internal"
"github.com/BurntSushi/toml"
)
type StagingMethod string
const (
StagingMethodNone = "none"
StagingMethodTemporary = "temporary"
StagingMethodPersistent = "persistent"
)
// Global parameters
type GlobalConfig struct {
// MaxInterval is the maximum time to wait before syncing with a
// mirror. The wait time is a randomly generated time between the Min
// and Max intervals. The mirror is immediately downloaded once if both
// the Min and Max intervals are set to zero.
MaxInterval *Duration `toml:"max-interval"`
// MinInterval is the minimum time to wait before syncing with a mirror.
MinInterval *Duration `toml:"min-interval"`
StagingMethod string `toml:"staging-method,omitempty"`
StagingPath string `toml:"staging-path,omitempty"`
Verify string `toml:"verify,omitempty"`
}
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: DurationRef(24 * time.Hour),
MinInterval: DurationRef(time.Hour),
StagingMethod: StagingMethodNone,
StagingPath: "/tmp/mirror",
Verify: "",
},
}
// Read the given configuration file.
func readConfigFile(fp string) (*Config, error) {
var config Config
f, err := os.Open(fp)
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
}
// Read all configuration in the given directory.
func readConfigDir(fp string) (*Config, error) {
var cfg Config
confDDir, err := os.ReadDir(fp)
if os.IsNotExist(err) {
// No directory is an empty one.
return &Config{}, nil
} else if err != nil {
return nil, err
}
for _, entry := range confDDir {
if filepath.Ext(entry.Name()) != ".toml" {
continue
}
entryCfg, err := readConfigFile(filepath.Join(fp, entry.Name()))
if err != nil {
return nil, err
}
cfg.Append(entryCfg)
}
return &cfg, nil
}
// Read all configuration in the given directory.
func ReadConfig(fps ...string) (*Config, error) {
var cfg Config
for _, fp := range fps {
stat, err := os.Stat(fp)
if err != nil {
return nil, err
}
var c *Config
if stat.IsDir() {
c, err = readConfigDir(fp)
if err != nil {
return nil, err
}
} else {
c, err = readConfigFile(fp)
}
cfg.Append(c)
}
return &cfg, nil
}
// Apply the given configuration parameters.
func (c *Config) Append(src *Config) {
if src.MaxInterval != nil {
c.MaxInterval = src.MaxInterval
}
if src.MinInterval != nil {
c.MinInterval = src.MinInterval
}
if src.StagingMethod != "" {
c.StagingMethod = src.StagingMethod
}
if src.StagingPath != "" {
c.StagingPath = src.StagingPath
}
c.Mirrors = append(c.Mirrors, src.Mirrors...)
}
|