-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrunner.go
272 lines (181 loc) · 4.76 KB
/
runner.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package runner
import (
"github.com/v8platform/errors"
"github.com/v8platform/find"
"github.com/v8platform/runner/cmd"
"strings"
"context"
)
const CreateInfobase = "CREATEINFOBASE"
var defaultVersion = "8.3"
type PlatformRunner interface {
//CreateInfobase()
Run(ctx context.Context) error
Background(ctx context.Context) (Process, error)
Check() error
Args() []string
Opts() Options
}
func NewPlatformRunner(where Infobase, what Command, opts ...interface{}) PlatformRunner {
runner := newRunner(where, what, opts...)
return &runner
}
type platformRunner struct {
Options *Options
Where Infobase
What Command
command string
args []string
}
func (r *platformRunner) Run(ctx context.Context) error {
p, err := r.Background(ctx)
if err != nil {
return err
}
return <-p.Wait()
}
func (r *platformRunner) Background(ctx context.Context) (Process, error) {
if err := r.Check(); err != nil {
return nil, err
}
var err error
r.command, err = getV8Path(*r.Options)
if err != nil {
return nil, err
}
p := r.background(ctx)
return p, nil
}
func (r *platformRunner) Check() error {
_, err := getV8Path(*r.Options)
if err != nil {
return err
}
return checkCommand(r.What)
}
func (r *platformRunner) Args() []string {
commandV8, _ := getV8Path(*r.Options)
return append([]string{
commandV8}, r.args...)
}
func (r *platformRunner) Opts() Options {
return *r.Options
}
func newRunner(where Infobase, what Command, opts ...interface{}) platformRunner {
options := defaultOptions()
inlineOptions := getOptions(opts...)
if inlineOptions != nil {
options = inlineOptions
}
o := clearOpts(opts)
options.Options(o...)
args := getCmdArgs(where, what, *options)
r := platformRunner{
Where: where,
What: what,
Options: options,
args: args,
}
return r
}
func Run(where Infobase, what Command, opts ...interface{}) error {
return NewPlatformRunner(where, what, opts...).Run(context.Background())
}
func Background(ctx context.Context, where Infobase, what Command, opts ...interface{}) (Process, error) {
return NewPlatformRunner(where, what, opts...).Background(ctx)
}
func (r *platformRunner) background(ctx context.Context) Process {
if r.Options.Context == nil {
r.Options.Context = ctx
}
cmdRunner := cmd.NewCmdRunner(r.command, r.args,
cmd.WithContext(r.Options.Context),
cmd.WithOutFilePath(r.Options.Out),
cmd.WithDumpResultFilePath(r.Options.DumpResult),
)
p := background(cmdRunner, ctx)
return p
}
func checkCommand(what Command) (err error) {
err = what.Check()
return
}
func isCreateInfobase(what Command) bool {
return strings.EqualFold(strings.ToUpper(what.Command()), CreateInfobase)
}
func getConnectionsStringParams(values []string) (params []string, additional []string) {
for _, value := range values {
if strings.HasPrefix(value, "/") || strings.HasPrefix(value, "-") {
additional = append(additional, value)
} else {
params = append(params, value)
}
}
return
}
func joinConnectionStringParams(whereParams, whatParams []string) string {
// TODO Сделать поиск одинаковых параметров
params := append(whereParams, whatParams...)
return strings.Join(params, ";")
}
func getCmdArgs(where Infobase, what Command, options Options) []string {
params := &Params{
values: []string{what.Command()},
}
if isCreateInfobase(what) {
connectionStringParams, values := getConnectionsStringParams(what.Values())
connectionString := strings.Join(connectionStringParams, ";")
params.Append(connectionString)
params.Append(values...)
} else {
params.Append(where.ConnectionString())
params.Append(what.Values()...)
}
params.Append(options.commonValues...)
params.Append(options.Values()...)
return params.Values()
}
func getV8Path(options Options) (string, error) {
if len(options.v8path) > 0 {
return options.v8path, nil
}
v8 := defaultVersion
if len(options.Version) > 0 {
v8 = options.Version
}
v8path, err := find.PlatformByVersion(v8, find.WithBitness(find.V8_x64x32))
if err != nil {
err = errors.NotExist.Newf("Version %s not found", options.Version)
_ = errors.AddErrorContext(err, "version", options.Version)
return "", err
}
return v8path, nil
}
func defaultOptions() *Options {
options := Options{}
options.NewOutFile()
options.NewDumpResultFile()
//options.customValues = *types.NewValues()
//options.commonValues = *types.NewValues()
return &options
}
func getOptions(opts ...interface{}) *Options {
for _, opt := range opts {
switch o := opt.(type) {
case Options:
return &o
case *Options:
return o
}
}
return nil
}
func clearOpts(opts []interface{}) []Option {
var o []Option
for _, opt := range opts {
if fn, ok := opt.(Option); ok {
o = append(o, fn)
}
}
return o
}