-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
84 lines (71 loc) · 1.83 KB
/
config.go
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
package main
import (
"fmt"
"log"
"github.com/BurntSushi/toml"
)
// LoadConfig loads the config file from the given path and returns the configuration information,
// panicking on error.
func LoadConfig(path string) (MysqlConfig, AwsConfig, map[Frequency]ScheduleConfig) {
var config Config
_, err := toml.DecodeFile(path, &config)
if err != nil {
log.Panicln(err)
}
schedule, err := config.Schedule()
if err != nil {
log.Panicln(err)
}
return config.Mysql, config.Aws, schedule
}
// Config is the global configuration for backups.
type Config struct {
Mysql MysqlConfig `toml:"mysql"`
Aws AwsConfig `toml:"aws"`
ScheduleInner map[string]ScheduleConfig `toml:"schedule"`
}
func (c Config) Schedule() (map[Frequency]ScheduleConfig, error) {
schedule := make(map[Frequency]ScheduleConfig, len(c.ScheduleInner))
for k, v := range c.ScheduleInner {
var k2 Frequency
if err := k2.UnmarshalText([]byte(k)); err != nil {
return nil, err
}
schedule[k2] = v
}
return schedule, nil
}
// MysqlConfig is the MySQL-specific configuration.
type MysqlConfig struct {
Host string `toml:"host"`
Port uint16 `toml:"port"`
User string `toml:"user"`
Pass string `toml:"pass"`
}
// AwsConfig is the AWS-specific configuration.
type AwsConfig struct {
SecretKey string `toml:"secret_key"`
S3Bucket string `toml:"s3_bucket"`
}
// ScheduleConfig is the configuration for a single item in the schedule.
type ScheduleConfig struct {
Incremental bool `toml:"incremental"`
}
type Frequency struct {
code int
}
func (f *Frequency) UnmarshalText(text []byte) error {
textStr := string(text)
if textStr == "daily" {
f.code = 0
} else if textStr == "weekly" {
f.code = 1
} else if textStr == "monthly" {
f.code = 2
} else if textStr == "yearly" {
f.code = 3
} else {
return fmt.Errorf("Unknown frequency: %s", text)
}
return nil
}