-
Notifications
You must be signed in to change notification settings - Fork 801
/
Copy pathconfig.go
95 lines (73 loc) · 2.11 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
85
86
87
88
89
90
91
92
93
94
95
package river
import (
"io/ioutil"
"time"
"github.com/BurntSushi/toml"
"github.com/juju/errors"
)
// SourceConfig is the configs for source
type SourceConfig struct {
Schema string `toml:"schema"`
Tables []string `toml:"tables"`
}
// Config is the configuration
type Config struct {
MyAddr string `toml:"my_addr"`
MyUser string `toml:"my_user"`
MyPassword string `toml:"my_pass"`
MyCharset string `toml:"my_charset"`
MyTimezone TomeLocation `toml:"my_timezone"`
ESHttps bool `toml:"es_https"`
ESAddr string `toml:"es_addr"`
ESUser string `toml:"es_user"`
ESPassword string `toml:"es_pass"`
StatAddr string `toml:"stat_addr"`
ServerID uint32 `toml:"server_id"`
Flavor string `toml:"flavor"`
DataDir string `toml:"data_dir"`
DumpExec string `toml:"mysqldump"`
SkipMasterData bool `toml:"skip_master_data"`
Sources []SourceConfig `toml:"source"`
Rules []*Rule `toml:"rule"`
BulkSize int `toml:"bulk_size"`
FlushBulkTime TomlDuration `toml:"flush_bulk_time"`
SkipNoPkTable bool `toml:"skip_no_pk_table"`
}
// NewConfigWithFile creates a Config from file.
func NewConfigWithFile(name string) (*Config, error) {
data, err := ioutil.ReadFile(name)
if err != nil {
return nil, errors.Trace(err)
}
return NewConfig(string(data))
}
// NewConfig creates a Config from data.
func NewConfig(data string) (*Config, error) {
var c Config
c.MyTimezone = TomeLocation{time.Local}
_, err := toml.Decode(data, &c)
if err != nil {
return nil, errors.Trace(err)
}
return &c, nil
}
// TomlDuration supports time codec for TOML format.
type TomlDuration struct {
time.Duration
}
// UnmarshalText implementes TOML UnmarshalText
func (d *TomlDuration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
// TomeLocation supports time.Location codec for TOML format.
type TomeLocation struct {
*time.Location
}
// UnmarshalText implementes TOML UnmarshalText
func (l *TomeLocation) UnmarshalText(text []byte) error {
var err error
l.Location, err = time.LoadLocation(string(text))
return err
}