-
Notifications
You must be signed in to change notification settings - Fork 17
/
help.go
112 lines (100 loc) · 3.16 KB
/
help.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
package baker
import (
"fmt"
"io"
"strings"
)
// HelpFormat represents the possible formats for baker help.
type HelpFormat int
const (
// HelpFormatRaw is for raw-formatted help.
HelpFormatRaw HelpFormat = iota
// HelpFormatMarkdown is for markdown formatted help.
HelpFormatMarkdown
)
// PrintHelp prints the help message for the given component, identified by its name.
// When name is '*' it shows the help messages for all components.
//
// The help message includes the component's description as well as the help messages
// for all component's configuration keys.
//
// An example of usage is:
//
// var flagPrintHelp = flag.String("help", "", "show help for a `component` ('*' for all)")
// flag.Parse()
// comp := baker.Components{ /* add all your baker components here */ }
// PrintHelp(os.Stderr, *flagPrintHelp, comp)
//
// Help output example:
//
// $ ./baker-bin -help TCP
//
// =============================================
// Input: TCP
// =============================================
// This input relies on a TCP connection to receive records in the usual format
// Configure it with a host and port that you want to accept connection from.
// By default it listens on port 6000 for any connection
// It never exits.
//
// Keys available in the [input.config] section:
//
// Name | Type | Default | Help
// ----------------------------------------------------------------------------------------------------
// Listener | string | | Host:Port to bind to
// ----------------------------------------------------------------------------------------------------
func PrintHelp(w io.Writer, name string, comp Components, format HelpFormat) error {
dumpall := name == "*"
generateHelp := GenerateTextHelp
if format == HelpFormatMarkdown {
generateHelp = GenerateMarkdownHelp
}
for _, inp := range comp.Inputs {
if strings.EqualFold(inp.Name, name) || dumpall {
if err := generateHelp(w, inp); err != nil {
return fmt.Errorf("can't print help for %q input: %v", inp.Name, err)
}
if !dumpall {
return nil
}
}
}
for _, fil := range comp.Filters {
if strings.EqualFold(fil.Name, name) || dumpall {
if err := generateHelp(w, fil); err != nil {
return fmt.Errorf("can't print help for %q filter: %v", fil.Name, err)
}
if !dumpall {
return nil
}
}
}
for _, out := range comp.Outputs {
if strings.EqualFold(out.Name, name) || dumpall {
if strings.EqualFold(out.Name, name) || dumpall {
if err := generateHelp(w, out); err != nil {
return fmt.Errorf("can't print help for %q output: %v", out.Name, err)
}
if !dumpall {
return nil
}
}
}
}
for _, upl := range comp.Uploads {
if strings.EqualFold(upl.Name, name) || dumpall {
if strings.EqualFold(upl.Name, name) || dumpall {
if err := generateHelp(w, upl); err != nil {
return fmt.Errorf("can't print help for %q upload: %v", upl.Name, err)
}
if !dumpall {
return nil
}
}
}
}
if !dumpall {
return fmt.Errorf("component not found: %s", name)
}
return nil
}