/
control.go
351 lines (292 loc) · 8.19 KB
/
control.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
// Copyright 2018 Istio Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collateral
import (
"bytes"
"fmt"
"html"
"os"
"sort"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/pflag"
)
// Control determines the behavior of the EmitCollateral function
type Control struct {
// OutputDir specifies the directory to output the collateral files
OutputDir string
// EmitManPages controls whether to produce man pages.
EmitManPages bool
// EmitYAML controls whether to produce YAML files.
EmitYAML bool
// EmitBashCompletion controls whether to produce bash completion files.
EmitBashCompletion bool
// EmitMarkdown controls whether to produce markdown documentation files.
EmitMarkdown bool
// EmitHTMLFragmentWithFrontMatter controls whether to produce HTML fragments with Jekyll/Hugo front matter.
EmitHTMLFragmentWithFrontMatter bool
// ManPageInfo provides extra information necessary when emitting man pages.
ManPageInfo doc.GenManHeader
}
// EmitCollateral produces a set of collateral files for a CLI command. You can
// select to emit markdown to describe a command's function, man pages, YAML
// descriptions, and bash completion files.
func EmitCollateral(root *cobra.Command, c *Control) error {
if c.EmitManPages {
if err := doc.GenManTree(root, &c.ManPageInfo, c.OutputDir); err != nil {
return fmt.Errorf("unable to output manpage tree: %v", err)
}
}
if c.EmitMarkdown {
if err := doc.GenMarkdownTree(root, c.OutputDir); err != nil {
return fmt.Errorf("unable to output markdown tree: %v", err)
}
}
if c.EmitHTMLFragmentWithFrontMatter {
if err := genHTMLFragment(root, c.OutputDir+"/"+root.Name()+".html"); err != nil {
return fmt.Errorf("unable to output HTML fragment file: %v", err)
}
}
if c.EmitYAML {
if err := doc.GenYamlTree(root, c.OutputDir); err != nil {
return fmt.Errorf("unable to output YAML tree: %v", err)
}
}
if c.EmitBashCompletion {
if err := root.GenBashCompletionFile(c.OutputDir + "/" + root.Name() + ".bash"); err != nil {
return fmt.Errorf("unable to output bash completion file: %v", err)
}
}
return nil
}
type generator struct {
buffer *bytes.Buffer
}
func (g *generator) emit(str ...string) {
for _, s := range str {
g.buffer.WriteString(s)
}
g.buffer.WriteByte('\n')
}
func findCommands(commands map[string]*cobra.Command, cmd *cobra.Command) {
cmd.InitDefaultHelpCmd()
cmd.InitDefaultHelpFlag()
commands[cmd.CommandPath()] = cmd
for _, c := range cmd.Commands() {
findCommands(commands, c)
}
}
const help = "help"
func genHTMLFragment(cmd *cobra.Command, path string) error {
commands := make(map[string]*cobra.Command)
findCommands(commands, cmd)
names := make([]string, len(commands))
i := 0
for n := range commands {
names[i] = n
i++
}
sort.Strings(names)
g := &generator{
buffer: &bytes.Buffer{},
}
count := 0
for _, n := range names {
if commands[n].Name() == help {
continue
}
count++
}
g.genFileHeader(cmd, count)
for _, n := range names {
if commands[n].Name() == help {
continue
}
g.genCommand(commands[n])
}
f, err := os.Create(path)
if err != nil {
return err
}
_, err = g.buffer.WriteTo(f)
_ = f.Close()
return err
}
func (g *generator) genFileHeader(root *cobra.Command, numEntries int) {
g.emit("---")
g.emit("title: ", root.Name())
g.emit("description: ", root.Short)
g.emit("generator: pkg-collateral-docs")
g.emit("number_of_entries: ", strconv.Itoa(numEntries))
g.emit("---")
}
func (g *generator) genCommand(cmd *cobra.Command) {
if cmd.Hidden || cmd.Deprecated != "" {
return
}
if cmd.HasParent() {
g.emit("<h2 id=\"", normalizeID(cmd.CommandPath()), "\">", cmd.CommandPath(), "</h2>")
}
if cmd.Long != "" {
g.emitText(cmd.Long)
} else if cmd.Short != "" {
g.emitText(cmd.Short)
}
if cmd.Runnable() {
g.emit("<pre class=\"language-bash\"><code>", html.EscapeString(cmd.UseLine()))
g.emit("</code></pre>")
if len(cmd.Aliases) > 0 {
// first word in cmd.Use represents the command that is being aliased
word := cmd.Use
index := strings.Index(word, " ")
if index > 0 {
word = word[0:index]
}
g.emit("<div class=\"aliases\">")
line := cmd.UseLine()
for i, alias := range cmd.Aliases {
r := strings.Replace(line, word, alias, 1)
if i == 0 {
g.emit("<pre class=\"language-bash\"><code>", html.EscapeString(r))
} else {
g.emit(html.EscapeString(r))
}
}
g.emit("</code></pre></div>")
}
}
flags := cmd.NonInheritedFlags()
flags.SetOutput(g.buffer)
parentFlags := cmd.InheritedFlags()
parentFlags.SetOutput(g.buffer)
if flags.HasFlags() || parentFlags.HasFlags() {
f := make(map[string]*pflag.Flag)
addFlags(f, flags)
addFlags(f, parentFlags)
if len(f) > 0 {
names := make([]string, len(f))
i := 0
for n := range f {
names[i] = n
i++
}
sort.Strings(names)
genShorthand := false
for _, v := range f {
if v.Shorthand != "" && v.ShorthandDeprecated == "" {
genShorthand = true
break
}
}
g.emit("<table class=\"command-flags\">")
g.emit("<thead>")
g.emit("<th>Flags</th>")
if genShorthand {
g.emit("<th>Shorthand</th>")
}
g.emit("<th>Description</th>")
g.emit("</thead>")
g.emit("<tbody>")
for _, n := range names {
g.genFlag(f[n], genShorthand)
}
g.emit("</tbody>")
g.emit("</table>")
}
}
if len(cmd.Example) > 0 {
g.emit("<h3 id=\"", normalizeID(cmd.CommandPath()), " Examples\">", "Examples", "</h3>")
g.emit("<pre class=\"language-bash\"><code>", html.EscapeString(cmd.Example))
g.emit("</code></pre>")
}
}
func addFlags(f map[string]*pflag.Flag, s *pflag.FlagSet) {
s.VisitAll(func(flag *pflag.Flag) {
if flag.Deprecated != "" || flag.Hidden {
return
}
if flag.Name == help {
return
}
f[flag.Name] = flag
})
}
func (g *generator) genFlag(flag *pflag.Flag, genShorthand bool) {
varname, usage := unquoteUsage(flag)
if varname != "" {
varname = " <" + varname + ">"
}
def := ""
if flag.Value.Type() == "string" {
def = fmt.Sprintf(" (default `%s`)", flag.DefValue)
} else if flag.Value.Type() != "bool" {
def = fmt.Sprintf(" (default `%s`)", flag.DefValue)
}
g.emit("<tr>")
g.emit("<td><code>", "--", flag.Name, html.EscapeString(varname), "</code></td>")
if genShorthand {
if flag.Shorthand != "" && flag.ShorthandDeprecated == "" {
g.emit("<td><code>", "-", flag.Shorthand, "</code></td>")
} else {
g.emit("<td></td>")
}
}
g.emit("<td>", html.EscapeString(usage), " ", def, "</td>")
g.emit("</tr>")
}
func (g *generator) emitText(text string) {
paras := strings.Split(text, "\n\n")
for _, p := range paras {
g.emit("<p>", html.EscapeString(p), "</p>")
}
}
// unquoteUsage extracts a back-quoted name from the usage
// string for a flag and returns it and the un-quoted usage.
// Given "a `name` to show" it returns ("name", "a name to show").
// If there are no back quotes, the name is an educated guess of the
// type of the flag's value, or the empty string if the flag is boolean.
func unquoteUsage(flag *pflag.Flag) (name string, usage string) {
// Look for a back-quoted name, but avoid the strings package.
usage = flag.Usage
for i := 0; i < len(usage); i++ {
if usage[i] == '`' {
for j := i + 1; j < len(usage); j++ {
if usage[j] == '`' {
name = usage[i+1 : j]
usage = usage[:i] + name + usage[j+1:]
return name, usage
}
}
break // Only one back quote; use type name.
}
}
name = flag.Value.Type()
switch name {
case "bool":
name = ""
case "float64":
name = "float"
case "int64":
name = "int"
case "uint64":
name = "uint"
}
return
}
func normalizeID(id string) string {
id = strings.Replace(id, " ", "-", -1)
return strings.Replace(id, ".", "-", -1)
}