-
Notifications
You must be signed in to change notification settings - Fork 72
/
docs_index.go
104 lines (86 loc) · 2.2 KB
/
docs_index.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
package doc
import (
"os"
"path"
"path/filepath"
"text/template"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const contentTemplate = `:context: rhoas-cli-command-reference
[id="cli-command-reference_{context}"]
= CLI command reference (rhoas)
[role="_abstract"]
You use the ` + "`rhoas`" + ` CLI to manage your application services from the command line.
{{ range .Groups}}
== {{.Description}}
{{ range .Commands}}
include::{{.}}[leveloffset=+2]
{{ end }}
{{ end }}
`
type CommandGroup struct {
Description string
Commands []string
}
func CreateIndexFile(rootCmd *cobra.Command, generationOptions *GeneratorOptions) error {
var groups []CommandGroup = []CommandGroup{
{
Description: "General commands",
Commands: []string{},
},
}
children := rootCmd.Commands()
for _, child := range children {
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}
if child.Annotations[AnnotationName] != "" {
groupNames := CollectNames(child, generationOptions)
groups = append(groups, CommandGroup{
Description: child.Annotations[AnnotationName],
Commands: groupNames,
})
} else {
groupNames := CollectNames(child, generationOptions)
// Add to general command
groups[0].Commands = append(groups[0].Commands, groupNames...)
}
}
type Vars struct {
Groups []CommandGroup
}
vars := Vars{
Groups: groups,
}
output, err := os.Create(generationOptions.IndexFile)
if err != nil {
return errors.WithStack(err)
}
err = template.Must(template.New("content").Parse(contentTemplate)).Execute(output, vars)
if err != nil {
return errors.WithStack(err)
}
err = output.Sync()
if err != nil {
return errors.WithStack(err)
}
return nil
}
func CollectNames(cmd *cobra.Command, options *GeneratorOptions) []string {
filename := options.FileNameGenerator(cmd)
filename, err := filepath.Rel(path.Dir(options.IndexFile), filename)
if err != nil {
panic(err)
}
names := []string{filename}
if cmd.HasSubCommands() {
for _, sub := range cmd.Commands() {
if sub.IsAvailableCommand() && !sub.IsAdditionalHelpTopicCommand() {
subNames := CollectNames(sub, options)
names = append(names, subNames...)
}
}
}
return names
}