This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 276
/
generate.go
82 lines (65 loc) · 1.87 KB
/
generate.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package docs
import (
"bytes"
"fmt"
"io"
"sort"
"strings"
"github.com/spf13/cobra"
)
type byName []*cobra.Command
func (s byName) Len() int { return len(s) }
func (s byName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byName) Less(i, j int) bool { return s[i].CommandPath() < s[j].CommandPath() }
// Generate prints API docs for a command
func Generate(cmd *cobra.Command) string {
buf := new(bytes.Buffer)
cmds := genCmdList(cmd)
sort.Sort(byName(cmds))
for _, cmd := range cmds {
if len(strings.Split(cmd.CommandPath(), " ")) == 1 {
fmt.Fprint(buf, "**Options**\n\n")
printOptions(buf, cmd)
fmt.Fprintln(buf)
continue
}
depth := len(strings.Split(cmd.CommandPath(), " "))
fmt.Fprint(buf, header(depth, cmd.CommandPath()))
fmt.Fprint(buf, cmd.Long, "\n\n")
if cmd.Runnable() {
fmt.Fprint(buf, "**Usage:** ", "`", cmd.UseLine(), "`", "\n\n")
}
if cmd.HasLocalFlags() || cmd.HasPersistentFlags() {
fmt.Fprint(buf, "**Options**\n\n")
printOptions(buf, cmd)
}
if cmd.Example != "" {
fmt.Fprint(buf, "**Example**\n\n")
fmt.Fprint(buf, "```", "\n", cmd.Example, "```", "\n\n")
}
}
return buf.String()
}
func genCmdList(cmd *cobra.Command) (cmds []*cobra.Command) {
cmds = append(cmds, cmd)
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
cmds = append(cmds, genCmdList(c)...)
}
return cmds
}
func header(depth int, header string) string {
return fmt.Sprint(strings.Repeat("#", depth), " ", header, "\n\n")
}
func printOptions(w io.Writer, cmd *cobra.Command) {
fmt.Fprintln(w, "```")
flags := cmd.NonInheritedFlags()
flags.SetOutput(w)
flags.PrintDefaults()
fmt.Fprintln(w, "```")
fmt.Fprintln(w, "")
}