-
Notifications
You must be signed in to change notification settings - Fork 26
/
list.go
103 lines (87 loc) · 2.78 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
package list
import (
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/algorand/conduit/conduit/pipeline"
"github.com/algorand/conduit/conduit/plugins"
)
// Command is the list command to embed in a root cobra command.
var Command = &cobra.Command{
Use: "list",
Short: "List all available Conduit plugins",
Long: `List all available Conduit plugins by type and a short description.
Use this utility to explore the plugins. Drill into each plugin to get a
sample configuration.
Example:
conduit list importers algod
conduit list processors filter_processor`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
printAll()
return nil
},
// Silence errors because our logger will catch and print any errors
SilenceErrors: true,
}
func makeDetailsCommand(pluginType string, data func() []plugins.Metadata) *cobra.Command {
return &cobra.Command{
Use: pluginType + "s",
Aliases: []string{pluginType},
Short: fmt.Sprintf("Usage details for %s plugins.", pluginType),
Long: fmt.Sprintf(`Usage details for %s plugins. Pass in a specific plugin as a positional argument for a sample configuration file.`, pluginType),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
printMetadata(os.Stdout, data(), 0)
} else {
printDetails(args[0], data())
}
},
}
}
func init() {
Command.AddCommand(makeDetailsCommand("importer", pipeline.ImporterMetadata))
Command.AddCommand(makeDetailsCommand("processor", pipeline.ProcessorMetadata))
Command.AddCommand(makeDetailsCommand("exporter", pipeline.ExporterMetadata))
}
func printDetails(name string, plugins []plugins.Metadata) {
for _, data := range plugins {
if data.Name == name {
fmt.Println(data.SampleConfig)
return
}
}
fmt.Printf("Plugin not found: %s\n", name)
}
func printMetadata(w io.Writer, plugins []plugins.Metadata, leftIndent int) {
sort.Slice(plugins, func(i, j int) bool {
return plugins[i].Name < plugins[j].Name
})
largestPluginNameSize := 0
for _, data := range plugins {
if len(data.Name) > largestPluginNameSize {
largestPluginNameSize = len(data.Name)
}
}
for _, data := range plugins {
depthStr := strings.Repeat(" ", leftIndent)
// pad with right with spaces
fmtString := fmt.Sprintf("%s%%-%ds - %%s\n", depthStr, largestPluginNameSize)
if data.Deprecated {
fmtString = "[DEPRECATED] " + fmtString
}
fmt.Fprintf(w, fmtString, data.Name, data.Description)
}
}
func printAll() {
fmt.Fprint(os.Stdout, "importers:\n")
printMetadata(os.Stdout, pipeline.ImporterMetadata(), 2)
fmt.Fprint(os.Stdout, "\nprocessors:\n")
printMetadata(os.Stdout, pipeline.ProcessorMetadata(), 2)
fmt.Fprint(os.Stdout, "\nexporters:\n")
printMetadata(os.Stdout, pipeline.ExporterMetadata(), 2)
}