-
Notifications
You must be signed in to change notification settings - Fork 17
/
help_markdown.go
172 lines (156 loc) · 4.59 KB
/
help_markdown.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
package baker
import (
"fmt"
"io"
"reflect"
"github.com/charmbracelet/glamour"
)
// RenderHelpMarkdown prints markdown formatted help for a single component or
// for all of them (with name = '*'), and renders it so that it can be
// printed on a terminal.
func RenderHelpMarkdown(w io.Writer, name string, comp Components) error {
r, _ := glamour.NewTermRenderer(
// detect background color and pick either the default dark or light theme
glamour.WithAutoStyle(),
// wrap output at specific width
glamour.WithWordWrap(int(terminalWidth())),
)
if err := PrintHelp(r, name, comp, HelpFormatMarkdown); err != nil {
return err
}
r.Close()
_, err := io.Copy(w, r)
return err
}
// GenerateMarkdownHelp generates markdown-formatted textual help for a Baker
// component from its description structure. Markdown is written into w.
func GenerateMarkdownHelp(w io.Writer, desc interface{}) error {
if desc == nil {
return fmt.Errorf("can't generate markdown help for a nil interface")
}
if reflect.TypeOf(desc).Kind() == reflect.Ptr {
// dereference pointer
desc = reflect.ValueOf(desc).Elem().Interface()
}
switch d := desc.(type) {
case InputDesc:
doc, err := newInputDoc(d)
if err != nil {
return err
}
genInputMarkdown(w, doc)
case FilterDesc:
doc, err := newFilterDoc(d)
if err != nil {
return err
}
genFilterMarkdown(w, doc)
case OutputDesc:
doc, err := newOutputDoc(d)
if err != nil {
return err
}
genOutputMarkdown(w, doc)
case UploadDesc:
doc, err := newUploadDoc(d)
if err != nil {
return err
}
genUploadMarkdown(w, doc)
case MetricsDesc:
doc, err := newMetricsDoc(d)
if err != nil {
return err
}
genMetricsMarkdown(w, doc)
default:
return fmt.Errorf("can't generate markdown help, unsupported type %T", desc)
}
return nil
}
func genInputMarkdown(w io.Writer, doc inputDoc) {
fmt.Fprintf(w, "## Input *%s*\n", doc.name)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Overview")
fmt.Fprintln(w, doc.help)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Configuration")
if len(doc.keys) == 0 {
fmt.Fprintf(w, "No configuration available")
} else {
fmt.Fprintf(w, "\nKeys available in the `[input.config]` section:\n\n")
genConfigKeysMarkdown(w, doc.keys)
}
}
func genFilterMarkdown(w io.Writer, doc filterDoc) {
fmt.Fprintf(w, "## Filter *%s*\n", doc.name)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Overview")
fmt.Fprintln(w, doc.help)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Configuration")
if len(doc.keys) == 0 {
fmt.Fprintf(w, "No configuration available")
} else {
fmt.Fprintf(w, "\nKeys available in the `[filter.config]` section:\n\n")
genConfigKeysMarkdown(w, doc.keys)
}
}
func genOutputMarkdown(w io.Writer, doc outputDoc) {
const (
rawString = "This is a *raw* output, for each record it receives a buffer containing the serialized record, plus a list holding a set of fields (`output.fields` in TOML)."
nonRawString = "This is a *non-raw* output, it doesn't receive whole records. Instead it receives a list of fields for each record (`output.fields` in TOML)."
)
fmt.Fprintf(w, "## Output *%s*\n", doc.name)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Overview")
if doc.raw {
fmt.Fprintln(w, rawString)
} else {
fmt.Fprintln(w, nonRawString)
}
fmt.Fprintln(w)
fmt.Fprintln(w)
fmt.Fprintln(w, doc.help)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Configuration")
if len(doc.keys) == 0 {
fmt.Fprintf(w, "No configuration available")
} else {
fmt.Fprintf(w, "\nKeys available in the `[output.config]` section:\n\n")
genConfigKeysMarkdown(w, doc.keys)
}
}
func genUploadMarkdown(w io.Writer, doc uploadDoc) {
fmt.Fprintf(w, "## Upload *%s*\n", doc.name)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Overview")
fmt.Fprintln(w, doc.help)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Configuration")
if len(doc.keys) == 0 {
fmt.Fprintf(w, "No configuration available")
} else {
fmt.Fprintf(w, "\nKeys available in the `[upload.config]` section:\n\n")
genConfigKeysMarkdown(w, doc.keys)
}
}
func genMetricsMarkdown(w io.Writer, doc metricsDoc) {
fmt.Fprintf(w, "## Metrics *%s*\n", doc.name)
fmt.Fprintln(w)
fmt.Fprintln(w, "### Configuration")
if len(doc.keys) == 0 {
fmt.Fprintf(w, "No configuration available")
} else {
fmt.Fprintf(w, "\nKeys available in the `[metrics.config]` section:\n\n")
genConfigKeysMarkdown(w, doc.keys)
}
}
func genConfigKeysMarkdown(w io.Writer, keys []helpConfigKey) {
fmt.Fprintln(w, "|Name|Type|Default|Required|Description|")
fmt.Fprintln(w, "|----|:--:|:-----:|:------:|-----------|")
for _, k := range keys {
fmt.Fprintf(w, "| %v| %v| %v| %t| %v|\n", k.name, k.typ, k.def, k.required, k.desc)
}
fmt.Fprintln(w)
}