/
plax_plugin.go
336 lines (275 loc) · 8.72 KB
/
plax_plugin.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package dsl
import (
"context"
"fmt"
"github.com/Comcast/plax/dsl"
"github.com/Comcast/plax/junit"
)
const (
// PluginDefNameKey of the PluginDef map
PluginDefNameKey = "Name"
// PluginDefDirKey of the PluginDef map
PluginDefDirKey = "Dir"
// PluginDefFilenameKey of the PluginDef map
PluginDefFilenameKey = "Filename"
// PluginDefParamsKey of the PluginDef map
PluginDefParamsKey = "Params"
// PluginDefSeedKey of the PluginDef map
PluginDefSeedKey = "Seed"
// PluginDefVerboseKey of the PluginDef map
PluginDefVerboseKey = "Verbose"
// PluginDefPriorityKey of the PluginDef map
PluginDefPriorityKey = "Priority"
// PluginDefLabelsKey of the PluginDef map
PluginDefLabelsKey = "Labels"
// PluginDefTestsKey of the PluginDef map
PluginDefTestsKey = "Tests"
// PluginDefLogLevelKey of the PluginDef map
PluginDefLogLevelKey = "LogLevels"
// PluginDefListKey of the PluginDef map
PluginDefListKey = "List"
// PluginDefEmitJSONKey of the PluginDef map
PluginDefEmitJSONKey = "EmitJSON"
// PluginDefEmitRedactKey = "EmitJSON"
PluginDefRedactKey = "Redact"
// PluginDefNonzeroOnAnyErrorKey of the PluginDef map
PluginDefNonzeroOnAnyErrorKey = "NonzeroOnAnyErrorKey"
// PluginDefRetryKey of the PluginDef map
PluginDefRetryKey = "Retry"
// PluginDefIncludeDirsKey of the PluginDef map
PluginDefIncludeDirsKey = "IncludeDirs"
)
var (
// DefaultPluginModule to load; "There can be only one!"
DefaultPluginModule PluginModule = "github.com/Comcast/plax"
)
// GetPluginDefName returns the Name
func (pd PluginDef) GetPluginDefName() (string, error) {
value, ok := pd[PluginDefNameKey]
if !ok {
return "", fmt.Errorf("%s was not provided in the plugin definition", PluginDefNameKey)
}
ret, ok := value.(string)
if !ok {
return "", fmt.Errorf("%s is not a string", PluginDefNameKey)
}
return ret, nil
}
// GetPluginDefTests returns the list of test to run
func (pd PluginDef) GetPluginDefTests() ([]string, error) {
value, ok := pd[PluginDefTestsKey]
if !ok {
return []string{}, fmt.Errorf("%s was not provided in the plugin definition", PluginDefTestsKey)
}
ret, ok := value.([]string)
if !ok {
return []string{}, fmt.Errorf("%s is not a []string", PluginDefTestsKey)
}
return ret, nil
}
// GetPluginDefDir returns the Dir
func (pd PluginDef) GetPluginDefDir() (string, error) {
value, ok := pd[PluginDefDirKey]
if !ok {
return "", fmt.Errorf("%s was not provided in the plugin definition", PluginDefDirKey)
}
ret, ok := value.(string)
if !ok {
return "", fmt.Errorf("%s is not a string", PluginDefDirKey)
}
return ret, nil
}
// GetPluginDefFilename returns the Filename
func (pd PluginDef) GetPluginDefFilename() (string, error) {
value, ok := pd[PluginDefFilenameKey]
if !ok {
return "", fmt.Errorf("%s was not provided in the plugin definition", PluginDefFilenameKey)
}
ret, ok := value.(string)
if !ok {
return "", fmt.Errorf("%s is not a string", PluginDefFilenameKey)
}
return ret, nil
}
// GetPluginDefParams returns the Params
func (pd PluginDef) GetPluginDefParams() (map[string]interface{}, error) {
value, ok := pd[PluginDefParamsKey]
if !ok {
return nil, fmt.Errorf("%s was not provided in the plugin definition", PluginDefParamsKey)
}
bss, ok := value.(*dsl.Bindings)
if !ok {
return nil, fmt.Errorf("%s is not a map[string]interface{}", PluginDefParamsKey)
}
ret := make(map[string]interface{})
for k, v := range *bss {
ret[k] = v
}
return ret, nil
}
// GetPluginDefSeed returns the Seed
func (pd PluginDef) GetPluginDefSeed() (int64, error) {
value, ok := pd[PluginDefSeedKey]
if !ok {
return -1, fmt.Errorf("%s was not provided in the plugin definition", PluginDefSeedKey)
}
ret, ok := value.(int64)
if !ok {
return -1, fmt.Errorf("%s is not a int64", PluginDefSeedKey)
}
return ret, nil
}
// GetPluginDefVerbose returns the Filename
func (pd PluginDef) GetPluginDefVerbose() (bool, error) {
value, ok := pd[PluginDefVerboseKey]
if !ok || value == nil {
return false, fmt.Errorf("%s was not provided in the plugin definition", PluginDefVerboseKey)
}
ret, ok := value.(*bool)
if !ok {
return false, fmt.Errorf("%s is not a bool", PluginDefVerboseKey)
}
return *ret, nil
}
// GetPluginDefLogLevel returns the Filename
func (pd PluginDef) GetPluginDefLogLevel() (string, error) {
value, ok := pd[PluginDefLogLevelKey]
if !ok || value == nil {
return "", fmt.Errorf("%s was not provided in the plugin definition", PluginDefLogLevelKey)
}
ret, ok := value.(*string)
if !ok {
return "", fmt.Errorf("%s is not a string", PluginDefLogLevelKey)
}
return *ret, nil
}
// GetPluginDefPriority returns the Priority
func (pd PluginDef) GetPluginDefPriority() (int, error) {
value, ok := pd[PluginDefPriorityKey]
if !ok {
return -1, fmt.Errorf("%s was not provided in the plugin definition", PluginDefPriorityKey)
}
ret, ok := value.(int)
if !ok {
return -1, fmt.Errorf("%s is not a int", PluginDefPriorityKey)
}
return ret, nil
}
// GetPluginDefLabels returns the Labels
func (pd PluginDef) GetPluginDefLabels() (string, error) {
value, ok := pd[PluginDefLabelsKey]
if !ok {
return "", fmt.Errorf("%s was not provided in the plugin definition", PluginDefLabelsKey)
}
ret, ok := value.(string)
if !ok {
return "", fmt.Errorf("%s is not a string", PluginDefLabelsKey)
}
return ret, nil
}
// GetPluginDefRetry returns the Retry
func (pd PluginDef) GetPluginDefRetry() (string, error) {
value, ok := pd[PluginDefRetryKey]
if !ok {
return "", fmt.Errorf("%s was not provided in the plugin definition", PluginDefRetryKey)
}
ret, ok := value.(string)
if !ok {
return "", fmt.Errorf("%s is not a string", PluginDefRetryKey)
}
return ret, nil
}
// GetPluginDefList returns the List flag
func (pd PluginDef) GetPluginDefList() (bool, error) {
value, ok := pd[PluginDefListKey]
if !ok {
return false, nil
}
ret, ok := value.(bool)
if !ok {
return false, fmt.Errorf("%s is not a bool", PluginDefListKey)
}
return ret, nil
}
// GetPluginDefRedact returns the Redact flag.
func (pd PluginDef) GetPluginDefRedact() (bool, error) {
value, ok := pd[PluginDefRedactKey]
if !ok || value == nil {
return false, fmt.Errorf("%s was not provided in the plugin definition", PluginDefRedactKey)
}
ret, ok := value.(*bool)
if !ok {
return false, fmt.Errorf("%s is not a bool", PluginDefRedactKey)
}
return *ret, nil
}
// GetPluginDefNonzeroOnAnyErrorKey returns the EmitJSON flag
func (pd PluginDef) GetPluginDefNonzeroOnAnyErrorKey() (bool, error) {
value, ok := pd[PluginDefNonzeroOnAnyErrorKey]
if !ok {
return false, nil
}
ret, ok := value.(bool)
if !ok {
return false, fmt.Errorf("%s is not a bool", PluginDefNonzeroOnAnyErrorKey)
}
return ret, nil
}
// GetPluginDefIncludeDirsKey returns the Includes list
func (pd PluginDef) GetPluginDefIncludeDirsKey() ([]string, error) {
value, ok := pd[PluginDefIncludeDirsKey]
if !ok || value == nil {
return []string{}, nil
}
ret, ok := value.(IncludeDirList)
if !ok {
return []string{}, fmt.Errorf("%s is not a IncludeDirList", PluginDefIncludeDirsKey)
}
return ret, nil
}
// PluginDef map
type PluginDef map[string]interface{}
// PluginOpts represents generic data that is given to a plugin
type PluginOpts interface{}
// PluginModule identifies the plax plugin by module name
type PluginModule string
// PluginMaker is the signature for a Plugin constructor
type PluginMaker func(def PluginDef) (Plugin, error)
// PluginRegistry maps a PluginModule to a constructor for that type of Plugin
type PluginRegistry map[PluginModule]PluginMaker
// Register registers a plugin
func (pr PluginRegistry) Register(module PluginModule, maker PluginMaker) {
pr[module] = maker
}
// ThePluginRegistry is the global, well-know registry of supported plax Plugin types
var ThePluginRegistry = make(PluginRegistry)
// MakePlugin create the plugin
func MakePlugin(module PluginModule, def PluginDef) (Plugin, error) {
maker, ok := ThePluginRegistry[module]
if !ok {
return nil, fmt.Errorf("plugin %s is not registered", module)
}
return maker(def)
}
// Plugin interface of invoking plax
type Plugin interface {
// Invoke calls the plugin
Invoke(ctx context.Context) (*junit.TestSuite, error)
}