forked from hidu/mysql-schema-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
90 lines (82 loc) · 1.99 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
package sync
import (
"strings"
"github.com/admpub/mysql-schema-sync/internal"
)
type Config struct {
Sync bool
Drop bool
SourceDSN string
DestDSN string
AlterIgnore string
Tables string
SkipTables string
MailTo string
SQLPreprocessor func(string) string `json:"-" xml:"-"`
Comparer internal.Comparer `json:"-" xml:"-"`
}
func (c *Config) ToConfig(mc *EmailConfig) (*internal.Config, error) {
cfg := new(internal.Config)
cfg.SourceDSN = c.SourceDSN
cfg.DestDSN = c.DestDSN
cfg.Sync = c.Sync
cfg.Drop = c.Drop
cfg.SetSQLPreprocessor(c.SQLPreprocessor)
cfg.SetComparer(c.Comparer)
c.AlterIgnore = strings.TrimSpace(c.AlterIgnore)
c.AlterIgnore = strings.TrimLeft(c.AlterIgnore, `{`)
c.AlterIgnore = strings.TrimRight(c.AlterIgnore, `}`)
c.AlterIgnore = strings.TrimRight(c.AlterIgnore, `,`)
if len(c.AlterIgnore) > 0 {
to := &map[string]*internal.AlterIgnoreTable{}
err := internal.ParseJSON(`{`+c.AlterIgnore+`}`, &to)
if err != nil {
return cfg, err
}
cfg.AlterIgnore = *to
}
if mc != nil {
cfg.Email = &internal.EmailStruct{
SendMailAble: mc.On,
SMTPHost: mc.SMTPHost,
From: mc.From,
Password: mc.Password,
To: mc.To,
}
}
if len(c.MailTo) > 0 && cfg.Email != nil {
cfg.Email.To = c.MailTo
}
if cfg.Tables == nil {
cfg.Tables = []string{}
}
if len(c.Tables) > 0 {
_ts := strings.Split(c.Tables, ",")
for _, _name := range _ts {
_name = strings.TrimSpace(_name)
if len(_name) > 0 {
cfg.Tables = append(cfg.Tables, _name)
}
}
}
if cfg.SkipTables == nil {
cfg.SkipTables = []string{}
}
if len(c.SkipTables) > 0 {
_ts := strings.Split(c.SkipTables, ",")
for _, _name := range _ts {
_name = strings.TrimSpace(_name)
if len(_name) > 0 {
cfg.SkipTables = append(cfg.SkipTables, _name)
}
}
}
return cfg, nil
}
type EmailConfig struct {
On bool
SMTPHost string
From string
Password string
To string
}