forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
123 lines (115 loc) · 2.57 KB
/
list.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
package cli
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"github.com/benthosdev/benthos/v4/internal/config/schema"
"github.com/benthosdev/benthos/v4/internal/cuegen"
)
func listCliCommand() *cli.Command {
return &cli.Command{
Name: "list",
Usage: "List all Benthos component types",
Description: `
If any component types are explicitly listed then only types of those
components will be shown.
benthos list
benthos list --format json inputs output
benthos list rate-limits buffers`[1:],
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Value: "text",
Usage: "Print the component list in a specific format. Options are text, json or cue.",
},
&cli.StringFlag{
Name: "status",
Value: "",
Usage: "Filter the component list to only those matching the given status. Options are stable, beta or experimental.",
},
},
Action: func(c *cli.Context) error {
listComponents(c)
os.Exit(0)
return nil
},
}
}
func listComponents(c *cli.Context) {
ofTypes := map[string]struct{}{}
for _, k := range c.Args().Slice() {
ofTypes[k] = struct{}{}
}
schema := schema.New(Version, DateBuilt)
if status := c.String("status"); status != "" {
schema.ReduceToStatus(status)
}
switch c.String("format") {
case "text":
flat := schema.Flattened()
i := 0
for _, k := range []string{
"inputs",
"processors",
"conditions",
"outputs",
"caches",
"rate-limits",
"buffers",
"metrics",
"tracers",
"bloblang-functions",
"bloblang-methods",
} {
if _, exists := ofTypes[k]; len(ofTypes) > 0 && !exists {
continue
}
if i > 0 {
fmt.Println("")
}
i++
title := cases.Title(language.English).String(strings.ReplaceAll(k, "-", " "))
fmt.Printf("%v:\n", title)
for _, t := range flat[k] {
fmt.Printf(" - %v\n", t)
}
}
case "json":
flat := schema.Flattened()
if len(ofTypes) > 0 {
for k := range flat {
if _, exists := ofTypes[k]; !exists {
delete(flat, k)
}
}
}
jsonBytes, err := json.Marshal(flat)
if err != nil {
panic(err)
}
fmt.Println(string(jsonBytes))
case "json-full":
jsonBytes, err := json.Marshal(schema)
if err != nil {
panic(err)
}
fmt.Println(string(jsonBytes))
case "json-full-scrubbed":
schema.Scrub()
jsonBytes, err := json.Marshal(schema)
if err != nil {
panic(err)
}
fmt.Println(string(jsonBytes))
case "cue":
source, err := cuegen.GenerateSchema(schema)
if err != nil {
panic(err)
}
fmt.Println(string(source))
}
}