forked from redhat-developer/odo
/
cli-doc.go
191 lines (155 loc) · 3.69 KB
/
cli-doc.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
package main
import (
"fmt"
"os"
"github.com/openshift/odo/pkg/odo/cli"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
/*
This "script" generates markdown that can be interpreted by the Slate (https://github.com/lord/slate) format.
Use this to script to generate the documentation needed.
*/
// Uses portions of the help / cmd outputter in cobra13 as part of a CLI reference guide and outputs each command
func referenceCommandFormatter(command *cobra.Command) string {
// Get length
var spacer string
for i := 0; i < len(command.Name()); i++ {
spacer = spacer + "~"
}
return fmt.Sprintf(`[[%s]]
%s
%s
[source,sh]
----
%s
----
_________________
Example using %s
_________________
[source,sh]
----
%s
----
%s
`,
command.Name(),
command.Name(),
spacer,
command.Use,
command.Name(),
command.Example,
command.Long)
}
// This prints out the CLI reference
func referencePrinter(command *cobra.Command, level int) string {
// Table generation
commandListTable := `
.List of Commands
[width="100%",cols="21%,79%",options="header",]
|===
| Name | Description
`
for _, subcommand := range command.Commands() {
commandListTable = commandListTable + fmt.Sprintf("| link:#%s[%s]\n| %s\n\n", subcommand.Name(), subcommand.Name(), subcommand.Short)
}
commandListTable = commandListTable + "|==="
// Create documentation for each command
var commandOutput string
for _, subcommand := range command.Commands() {
commandOutput = commandOutput + referenceCommandFormatter(subcommand)
}
// The main markdown "template" for everything
return fmt.Sprintf(`= Overview of the OpenShift Do (odo) CLI Structure
___________________
Example application
___________________
[source,sh]
----
%s
----
%s
[[syntax]]
Syntax
------
%s
[[cli-structure]]
CLI Structure
+++++++++++++
[source,sh]
----
%s
----
%s
`,
command.Example,
command.Long,
commandListTable,
fmt.Sprint(commandPrinter(command, 0)),
commandOutput)
}
func getFlags(flags *pflag.FlagSet) []string {
var f []string
flags.VisitAll(func(flag *pflag.Flag) {
f = append(f, fmt.Sprintf("--%v", flag.Name))
})
return f
}
func flattenFlags(flags []string) string {
var flagString string
for _, flag := range flags {
flagString = flagString + flag + " "
}
return flagString
}
func commandPrinter(command *cobra.Command, level int) string {
var finalCommand string
// add indentation
for i := 0; i < level; i++ {
finalCommand = finalCommand + " "
}
finalCommand = finalCommand +
command.Name() +
" " +
flattenFlags(getFlags(command.NonInheritedFlags())) +
": " +
command.Short +
"\n"
for _, subcommand := range command.Commands() {
finalCommand = finalCommand + commandPrinter(subcommand, level+1)
}
return finalCommand
}
// Generates and returns a markdown-formatted CLI reference page for Odo
func main() {
var clidoc = &cobra.Command{
Use: "cli-doc",
Short: "Generate CLI reference for odo",
Example: ` # Generate a markdown-formatted CLI reference page for Odo
cli-doc reference > docs/cli-reference.md
# Generate the CLI structure
cli-doc structure`,
Args: cobra.OnlyValidArgs,
ValidArgs: []string{"help", "reference", "structure"},
Run: func(command *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Print(command.Usage())
} else {
switch args[0] {
case "reference":
fmt.Print(referencePrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
case "structure":
fmt.Print(commandPrinter(cli.NewCmdOdo(cli.OdoRecommendedName, cli.OdoRecommendedName), 0))
default:
fmt.Print(command.Usage())
}
}
},
}
err := clidoc.Execute()
if err != nil {
fmt.Println(errors.Cause(err))
os.Exit(1)
}
}