-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfrepl.go
116 lines (93 loc) · 3.27 KB
/
confrepl.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
)
type replAccountsConf struct {
DstAcctpass []string `toml:"DstAcctpass,omitempty" json:"DstAcctpass,omitempty"`
SrcAcctpass []string `toml:"SrcAcctpass,omitempty" json:"SrcAcctpass,omitempty"`
Exclude []string `toml:"Exclude,omitempty" json:"Exclude,omitempty"`
}
func (a *replAccountsConf) normalize(index, sub int) error {
// if dst is not exist, is empty, or first index is empty, return error
if len(a.DstAcctpass) == 0 || a.DstAcctpass[0] == "" {
return fmt.Errorf("accounts %d-%d must define a destination", index, sub)
}
// if dst second index is not exist, set to empty
if len(a.DstAcctpass) < 2 {
a.DstAcctpass = []string{a.DstAcctpass[0], ""}
}
// if src is not exists, or is empty, inherit from dst
if len(a.SrcAcctpass) == 0 {
a.SrcAcctpass = a.DstAcctpass
}
// if src first index is empty, inherit from dst
if a.SrcAcctpass[0] == "" {
a.SrcAcctpass[0] = a.DstAcctpass[0]
}
// if src second index is not exist, or is empty, inherit from dst
if len(a.SrcAcctpass) < 2 || a.SrcAcctpass[1] == "" {
a.SrcAcctpass = []string{a.SrcAcctpass[0], a.DstAcctpass[1]}
}
return nil
}
type replServersConf struct {
DstSrvrport []string `toml:"DstSrvrport,omitempty" json:"DstSrvrport,omitempty"`
DstPathprefix string `toml:"DstPathprefix,omitempty" json:"DstPathprefix,omitempty"`
SrcSrvrport []string `toml:"SrcSrvrport,omitempty" json:"SrcSrvrport,omitempty"`
SrcPathprefix string `toml:"SrcPathprefix,omitempty" json:"SrcPathprefix,omitempty"`
Exclude []string `toml:"Exclude,omitempty" json:"Exclude,omitempty"`
Accounts []replAccountsConf `toml:"Accounts,omitempty" json:"Accounts,omitempty"`
}
func (g *replServersConf) normalize(index int) error {
// if dst is not exist, is empty, or first index is empty, return error
if len(g.DstSrvrport) == 0 || g.DstSrvrport[0] == "" {
return fmt.Errorf("servers %d must define a destination", index)
}
// if dst second index is not exist, or is empty, set to default
if len(g.DstSrvrport) < 2 || g.DstSrvrport[1] == "" {
g.DstSrvrport = []string{g.DstSrvrport[0], "993"}
}
// if src is not exists, or is empty, inherit from dst
if len(g.SrcSrvrport) == 0 {
g.SrcSrvrport = g.DstSrvrport
}
// if src first index is empty, inherit from dst
if g.SrcSrvrport[0] == "" {
g.SrcSrvrport[0] = g.DstSrvrport[0]
}
// if src second index is not exist, or is empty, inherit from dst
if len(g.SrcSrvrport) < 2 || g.SrcSrvrport[1] == "" {
g.SrcSrvrport = []string{g.SrcSrvrport[0], g.DstSrvrport[1]}
}
return nil
}
type replConf struct {
fs *flag.FlagSet
run bool
dstDebug bool
srcDebug bool
Servers []replServersConf `toml:"Servers,omitempty" json:"Servers,omitempty"`
}
func makeReplConf() replConf {
return replConf{
fs: flag.NewFlagSet("replicate", flag.ContinueOnError),
}
}
func (c *replConf) AttachFlags() {
c.fs.BoolVar(&c.dstDebug, "dst-debug", c.dstDebug, "enable dst client debugging")
c.fs.BoolVar(&c.srcDebug, "src-debug", c.srcDebug, "enable src client debugging")
}
func (c *replConf) Normalize() error {
for k := range c.Servers {
if err := c.Servers[k].normalize(k); err != nil {
return err
}
for i := range c.Servers[k].Accounts {
if err := c.Servers[k].Accounts[i].normalize(k, i); err != nil {
return err
}
}
}
return nil
}