forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deprecated.go
415 lines (385 loc) · 11.8 KB
/
deprecated.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package service
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"github.com/dafanshu/benthos/v3/lib/buffer"
"github.com/dafanshu/benthos/v3/lib/cache"
"github.com/dafanshu/benthos/v3/lib/condition"
"github.com/dafanshu/benthos/v3/lib/config"
"github.com/dafanshu/benthos/v3/lib/input"
"github.com/dafanshu/benthos/v3/lib/manager"
"github.com/dafanshu/benthos/v3/lib/metrics"
"github.com/dafanshu/benthos/v3/lib/output"
"github.com/dafanshu/benthos/v3/lib/processor"
"github.com/dafanshu/benthos/v3/lib/ratelimit"
"github.com/dafanshu/benthos/v3/lib/service/test"
"github.com/dafanshu/benthos/v3/lib/tracer"
uconfig "github.com/dafanshu/benthos/v3/lib/util/config"
)
//------------------------------------------------------------------------------
func cmdDeprecatedUnitTest(path, suffix string, doLint bool) {
if test.Run(path, suffix, doLint) {
os.Exit(0)
}
os.Exit(1)
}
func cmdDeprecatedUnitTestGenerate(path, suffix string) {
if err := test.Generate(path, suffix); err != nil {
fmt.Fprintf(os.Stderr, "Failed to generate config tests: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
func cmdDeprecatedLintConfig(lints []string) {
if len(lints) > 0 {
for _, l := range lints {
fmt.Fprintln(os.Stderr, l)
}
os.Exit(1)
}
os.Exit(0)
}
func cmdDeprecatedPrintConfig(conf *config.Type, examples string, showAll, jsonFormat bool) {
var outConf interface{}
var err error
if len(examples) > 0 {
config.AddExamples(conf, strings.Split(examples, ",")...)
}
if !showAll {
if outConf, err = conf.Sanitised(); err != nil {
fmt.Fprintf(os.Stderr, "Configuration sanitise error: %v\n", err)
os.Exit(1)
}
} else {
if len(conf.Input.Processors) == 0 &&
len(conf.Pipeline.Processors) == 0 &&
len(conf.Output.Processors) == 0 {
conf.Pipeline.Processors = append(conf.Pipeline.Processors, processor.NewConfig())
}
manager.AddExamples(&conf.Manager)
outConf = conf
}
if jsonFormat {
if configJSON, err := json.Marshal(outConf); err == nil {
fmt.Println(string(configJSON))
} else {
fmt.Fprintf(os.Stderr, "Configuration marshal error: %v\n", err)
}
} else {
if configYAML, err := uconfig.MarshalYAML(outConf); err == nil {
fmt.Println(string(configYAML))
} else {
fmt.Fprintf(os.Stderr, "Configuration marshal error: %v\n", err)
}
}
os.Exit(0)
}
func cmdDeprecatedPrintDocs(
printInputs bool,
printProcessors bool,
printConditions bool,
printRateLimits bool,
printBuffers bool,
printOutputs bool,
printCaches bool,
printMetrics bool,
printTracers bool,
printInputPlugins bool,
printProcessorPlugins bool,
printConditionPlugins bool,
printRateLimitPlugins bool,
printOutputPlugins bool,
printCachePlugins bool,
) {
if printInputs {
fmt.Println(input.Descriptions())
}
if printProcessors {
fmt.Println(processor.Descriptions())
}
if printConditions {
fmt.Println(condition.Descriptions())
}
if printRateLimits {
fmt.Println(ratelimit.Descriptions())
}
if printBuffers {
fmt.Println(buffer.Descriptions())
}
if printOutputs {
fmt.Println(output.Descriptions())
}
if printCaches {
fmt.Println(cache.Descriptions())
}
if printMetrics {
fmt.Println(metrics.Descriptions())
}
if printTracers {
fmt.Println(tracer.Descriptions())
}
if printInputPlugins {
fmt.Println(input.PluginDescriptions())
}
if printProcessorPlugins {
fmt.Println(processor.PluginDescriptions())
}
if printConditionPlugins {
fmt.Println(condition.PluginDescriptions())
}
if printRateLimitPlugins {
fmt.Println(ratelimit.PluginDescriptions())
}
if printOutputPlugins {
fmt.Println(output.PluginDescriptions())
}
if printCachePlugins {
fmt.Println(cache.PluginDescriptions())
}
os.Exit(0)
}
//------------------------------------------------------------------------------
var depFlags struct {
showConfigJSON bool
showConfigYAML bool
showAll bool
lintConfig bool
runTests string
generateTests string
strictConfig bool
examples string
printInputs bool
printOutputs bool
printBuffers bool
printProcessors bool
printConditions bool
printCaches bool
printRateLimits bool
printMetrics bool
printTracers bool
streamsMode bool
streamsDir string
// Plugin Flags
printInputPlugins bool
printOutputPlugins bool
printProcessorPlugins bool
printConditionPlugins bool
printCachePlugins bool
printRateLimitPlugins bool
}
func checkDeprecatedFlags(args []string) (*flag.FlagSet, bool) {
flagSet := flag.NewFlagSet("benthos", flag.ExitOnError)
flagSet.BoolVar(
&depFlags.streamsMode, "streams", false,
`
Run Benthos in streams mode, where streams can be created, updated and removed
via REST HTTP endpoints. In streams mode the stream fields of a config file
(input, buffer, pipeline, output) will be ignored. Instead, any .yaml or .json
files inside the --streams-dir directory will be parsed as stream configs.`[1:],
)
flagSet.StringVar(
&depFlags.streamsDir, "streams-dir", "",
`
When running Benthos in streams mode any files in this directory with a .json or
.yaml extension will be parsed as a stream configuration (input, buffer,
pipeline, output), where the filename less the extension will be the id of the
stream.`[1:],
)
flagSet.BoolVar(
&depFlags.showConfigJSON, "print-json", false, "Print loaded configuration as JSON, then exit",
)
flagSet.BoolVar(
&depFlags.showConfigYAML, "print-yaml", false, "Print loaded configuration as YAML, then exit",
)
flagSet.BoolVar(
&depFlags.showAll, "all", false,
`
Set whether all fields should be shown when printing configuration via
--print-yaml or --print-json, otherwise only used values will be printed.`[1:],
)
flagSet.BoolVar(
&depFlags.lintConfig, "lint", false, "Lint the target configuration file, then exit",
)
flagSet.StringVar(
&depFlags.runTests, "test", "",
`
EXPERIMENTAL: This flag is subject to change outside of major version releases.
Execute unit tests, then exit. The argument may point to a config file, test
definition or directory, and supports '...' wildcards, e.g. './foo/...' would
execute all tests found under the directory 'foo'. When combining this flag with
-lint each tested config will also be linted.`[1:],
)
flagSet.StringVar(
&depFlags.generateTests, "gen-test", "",
`
EXPERIMENTAL: This flag is subject to change outside of major version releases.
Generate unit test definition files for Benthos configs, then exit. The argument
may point to a config file or directory and supports '...' wildcards, e.g.
'./foo/...' would generate tests for all Benthos configs found under the
directory 'foo'.`[1:],
)
flagSet.BoolVar(
&depFlags.strictConfig, "strict", false,
`
Parse config files in strict mode, where any linting errors will cause Benthos
to fail`[1:],
)
flagSet.StringVar(
&depFlags.examples, "example", "",
`
Add specific examples when printing a configuration file with --print-yaml or
--print-json by listing comma separated types. Types can be any input, buffer,
processor or output.
For example: 'benthos --print-yaml --example websocket,jmespath' would print a
config with a websocket input and output and a jmespath processor.`[1:],
)
flagSet.BoolVar(
&depFlags.printInputs, "list-inputs", false,
"Print a list of available input options, then exit",
)
flagSet.BoolVar(
&depFlags.printOutputs, "list-outputs", false,
"Print a list of available output options, then exit",
)
flagSet.BoolVar(
&depFlags.printBuffers, "list-buffers", false,
"Print a list of available buffer options, then exit",
)
flagSet.BoolVar(
&depFlags.printProcessors, "list-processors", false,
"Print a list of available processor options, then exit",
)
flagSet.BoolVar(
&depFlags.printConditions, "list-conditions", false,
"Print a list of available processor condition options, then exit",
)
flagSet.BoolVar(
&depFlags.printCaches, "list-caches", false,
"Print a list of available cache options, then exit",
)
flagSet.BoolVar(
&depFlags.printRateLimits, "list-rate-limits", false,
"Print a list of available rate_limit options, then exit",
)
flagSet.BoolVar(
&depFlags.printMetrics, "list-metrics", false,
"Print a list of available metrics options, then exit",
)
flagSet.BoolVar(
&depFlags.printTracers, "list-tracers", false,
"Print a list of available tracer options, then exit",
)
if input.PluginCount() > 0 {
flagSet.BoolVar(
&depFlags.printInputPlugins, "list-input-plugins", false,
"Print a list of available input plugins, then exit",
)
}
if output.PluginCount() > 0 {
flagSet.BoolVar(
&depFlags.printOutputPlugins, "list-output-plugins", false,
"Print a list of available output plugins, then exit",
)
}
if processor.PluginCount() > 0 {
flagSet.BoolVar(
&depFlags.printProcessorPlugins, "list-processor-plugins", false,
"Print a list of available processor plugins, then exit",
)
}
if condition.PluginCount() > 0 {
flagSet.BoolVar(
&depFlags.printConditionPlugins, "list-condition-plugins", false,
"Print a list of available condition plugins, then exit",
)
}
if cache.PluginCount() > 0 {
flagSet.BoolVar(
&depFlags.printCachePlugins, "list-cache-plugins", false,
"Print a list of available cache plugins, then exit",
)
}
if ratelimit.PluginCount() > 0 {
flagSet.BoolVar(
&depFlags.printRateLimitPlugins, "list-rate-limit-plugins", false,
"Print a list of available ratelimit plugins, then exit",
)
}
foundOldFlag := false
flagSet.VisitAll(func(f *flag.Flag) {
if foundOldFlag {
return
}
for _, arg := range args {
if !strings.HasPrefix(arg, "-") {
continue
}
trimmed := strings.TrimPrefix(strings.TrimPrefix(arg, "--"), "-")
if f.Name == trimmed {
foundOldFlag = true
break
}
}
})
if foundOldFlag {
return flagSet, true
}
return nil, false
}
func deprecatedExecute(configPath, testSuffix string) {
fmt.Fprintln(os.Stderr, "Running with deprecated CLI flags, use --help to see an up to date summary.")
if len(depFlags.runTests) > 0 {
cmdDeprecatedUnitTest(depFlags.runTests, testSuffix, depFlags.lintConfig)
}
if len(depFlags.generateTests) > 0 {
cmdDeprecatedUnitTestGenerate(depFlags.generateTests, testSuffix)
}
if depFlags.lintConfig {
confReader := readConfig(configPath, false, nil, nil, nil)
lints, err := confReader.Read(&conf)
if err != nil {
fmt.Fprintf(os.Stderr, "Configuration file read error: %v\n", err)
os.Exit(1)
}
cmdDeprecatedLintConfig(lints)
}
// If the user wants the configuration to be printed we do so and then exit.
if depFlags.showConfigJSON || depFlags.showConfigYAML {
readConfig(configPath, false, nil, nil, nil)
cmdDeprecatedPrintConfig(&conf, depFlags.examples, depFlags.showAll, depFlags.showConfigJSON)
}
// If we only want to print our inputs or outputs we should exit afterwards
if depFlags.printInputs || depFlags.printOutputs || depFlags.printBuffers || depFlags.printProcessors ||
depFlags.printConditions || depFlags.printCaches || depFlags.printRateLimits ||
depFlags.printMetrics || depFlags.printTracers || depFlags.printInputPlugins ||
depFlags.printOutputPlugins || depFlags.printProcessorPlugins || depFlags.printConditionPlugins ||
depFlags.printCachePlugins || depFlags.printRateLimitPlugins {
cmdDeprecatedPrintDocs(
depFlags.printInputs,
depFlags.printProcessors,
depFlags.printConditions,
depFlags.printRateLimits,
depFlags.printBuffers,
depFlags.printOutputs,
depFlags.printCaches,
depFlags.printMetrics,
depFlags.printTracers,
depFlags.printInputPlugins,
depFlags.printProcessorPlugins,
depFlags.printConditionPlugins,
depFlags.printRateLimitPlugins,
depFlags.printOutputPlugins,
depFlags.printCachePlugins,
)
}
if depFlags.streamsMode {
dirs := []string{}
if len(depFlags.streamsDir) > 0 {
dirs = append(dirs, depFlags.streamsDir)
}
os.Exit(cmdService(configPath, nil, nil, "", depFlags.strictConfig, false, false, depFlags.streamsMode, dirs))
}
}