forked from onurcevik/DummyDump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy_dump.go
174 lines (143 loc) · 3.49 KB
/
dummy_dump.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package dummy_dump
import (
"github.com/sadihakan/dummy-dump/config"
"github.com/sadihakan/dummy-dump/errors"
"github.com/sadihakan/dummy-dump/internal"
"github.com/sadihakan/dummy-dump/util"
)
// DummyDump ..
type DummyDump struct {
c *config.Config
dump internal.Dump
Error error
}
// New ..
func New(cfg ...*config.Config) (*DummyDump, error) {
dd := new(DummyDump)
if len(cfg) > 0 {
dd.c = cfg[0]
if err := dd.configParser(); err != nil {
return nil, err
}
} else {
dd.c = new(config.Config)
}
return dd, nil
}
// SetUser ..
func (dd *DummyDump) SetUser(username, password string, sourceType config.SourceType) {
dd.c.Source = sourceType
dd.c.User = username
dd.c.Password = password
dd.configParserWithoutCheck()
}
// SetBinaryPath ..
func (dd *DummyDump) SetBinaryPath(binaryPath string, sourceType config.SourceType, importArg bool, exportArg bool) {
dd.c.Source = sourceType
dd.c.BinaryPath = binaryPath
dd.c.Export = exportArg
dd.c.Import = importArg
dd.configParserWithoutCheck()
}
// SetBinaryPath ..
func (dd *DummyDump) SetBinaryConfig(source config.SourceType, importArg bool, exportArg bool) {
dd.c.Source = source
dd.c.Import = importArg
dd.c.Export = exportArg
}
func (dd *DummyDump) configParserWithoutCheck() {
switch dd.c.Source {
case config.PostgreSQL:
dd.dump = internal.Postgres{}
break
case config.MySQL:
dd.dump = internal.MySQL{}
break
case config.MSSQL:
dd.dump = internal.MSSQL{}
case config.Oracle:
dd.dump = internal.Oracle{}
case config.Sqlite:
dd.dump = internal.Sqlite{}
case config.Sqlcompact:
dd.dump = internal.SqlCompact{}
default:
panic(errors.New("not implemented"))
}
}
func (dd *DummyDump) configParser() (err error) {
switch dd.c.Source {
case config.PostgreSQL:
if err = dd.c.CheckConfigPostgreSQL(); err != nil {
return err
}
dd.dump = internal.Postgres{}
break
case config.MySQL:
if err = dd.c.CheckConfigMySQL(); err != nil {
return err
}
dd.dump = internal.MySQL{}
break
case config.MSSQL:
if err = dd.c.CheckConfigMsSQL(); err != nil {
return err
}
dd.dump = internal.MSSQL{}
case config.Oracle:
if err = dd.c.CheckConfigOracle(); err != nil {
return err
}
dd.dump = internal.Oracle{}
case config.Sqlcompact:
dd.dump = internal.SqlCompact{}
case config.Sqlite:
dd.dump = internal.Sqlite{}
default:
err = errors.New("not implemented")
}
return err
}
func (dd *DummyDump) Import() *DummyDump {
dumpConfig := dd.c
if !util.PathExists(dumpConfig.BackupFilePath) && dd.Error == nil {
dd.Error = errors.New(errors.ConfigPathNotExist)
}
err := dd.dump.Import(*dumpConfig)
if err != nil && dd.Error == nil {
dd.Error = err
}
return dd
}
func (dd *DummyDump) Export() *DummyDump {
dumpConfig := dd.c
err := dd.dump.Export(*dumpConfig)
if err != nil && dd.Error == nil {
dd.Error = err
}
return dd
}
func (dd *DummyDump) Check() *DummyDump {
dd.Error = dd.dump.Check()
return dd
}
func (dd *DummyDump) CheckPath() *DummyDump {
dumpConfig := dd.c
dd.Error = dd.dump.CheckPath(*dumpConfig)
return dd
}
func (dd *DummyDump) Run() (*DummyDump, error) {
if dd.Error != nil {
return dd, dd.Error
}
return dd, nil
}
func (dd *DummyDump) GetBinary() (binaryPath string, version string) {
dumpConfig := dd.c
binaryPath, err := internal.CheckBinary(dumpConfig.BinaryPath, dumpConfig.Source, dumpConfig.Import, dumpConfig.Export)
version, err = internal.CheckVersion(binaryPath, dumpConfig.Source)
if err != nil {
dd.Error = err
}
return binaryPath, version
}