-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdocs.go
325 lines (277 loc) · 7.76 KB
/
cdocs.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
package cdocs
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"text/template"
"github.com/clok/kemba"
"github.com/cpuguy83/go-md2man/v2/md2man"
"github.com/urfave/cli/v2"
)
var (
kl = kemba.New("cdocs")
kim = kl.Extend("InstallManpageCommand")
kman = kl.Extend("install-manpage")
markdownDocTemplate = `% {{ .App.Name }} 8
# NAME
{{ .App.Name }}{{ if .App.Usage }} - {{ .App.Usage }}{{ end }}
# SYNOPSIS
{{ .App.Name }}
{{ if .SynopsisArgs }}
` + "```" + `
{{ range $v := .SynopsisArgs }}{{ $v }}{{ end }}` + "```" + `
{{ end }}{{ if .App.UsageText }}
# DESCRIPTION
{{ .App.UsageText }}
{{ end }}
# COMMAND TREE
{{ range $v := .TOC }}
{{ $v }}{{ end }}
**Usage**:
` + "```" + `
{{ .App.Name }} [GLOBAL OPTIONS] command [COMMAND OPTIONS] [ARGUMENTS...]
` + "```" + `
{{ if .GlobalArgs }}
# GLOBAL OPTIONS
{{ range $v := .GlobalArgs }}
{{ $v }}{{ end }}
{{ end }}{{ if .Commands }}
# COMMANDS
{{ range $v := .Commands }}
{{ $v }}{{ end }}{{ end }}`
)
// ToMarkdown creates a markdown string for the *cli.App
// The function errors if either parsing or writing of the string fails.
func ToMarkdown(a *cli.App) (string, error) {
var w bytes.Buffer
if err := writeDocTemplate(&w, a); err != nil {
return "", err
}
return w.String(), nil
}
// ToMan creates a man page string for the *cli.App
// The function errors if either parsing or writing of the string fails.
func ToMan(a *cli.App) (string, error) {
var w bytes.Buffer
if err := writeDocTemplate(&w, a); err != nil {
return "", err
}
man := md2man.Render(w.Bytes())
return string(man), nil
}
// InstallManpageCommandInput provides an interface to pass in options for the InstallManpageCommand
//
// - AppName is required.
//
// - CmdName defaults to 'install-manpage'
//
// - Path defaults to '/usr/local/share/man/man8'
type InstallManpageCommandInput struct {
AppName string `required:"true"`
CmdName string `default:"install-command"`
Path string `default:"/usr/local/share/man/man8"`
Hidden bool `default:"false"`
}
// InstallManpageCommand will generate a *cli.Command to be used with a cli.App.
// This will install a manual page (8) to the man-db.
func InstallManpageCommand(opts *InstallManpageCommandInput) (*cli.Command, error) {
name, cmdname, path, hidden, err := extractManpageSettings(opts)
if err != nil {
return nil, err
}
cmd := &cli.Command{
Name: cmdname,
Usage: "Generate and install man page",
UsageText: "NOTE: Windows is not supported",
Hidden: hidden,
Action: func(c *cli.Context) error {
kman.Printf("OS detected: %s", runtime.GOOS)
if runtime.GOOS == "windows" {
fmt.Println("Windows man page is not supported.")
return nil
}
if _, err := os.Stat(path); os.IsNotExist(err) {
return cli.Exit(fmt.Sprintf("Unable to install man page. %s does not exist", path), 2)
}
mp, _ := ToMan(c.App)
manpath := filepath.Join(path, fmt.Sprintf("%s.8", name))
kman.Printf("generated man page path: %s", manpath)
err := os.WriteFile(manpath, []byte(mp), 0644)
if err != nil {
return cli.Exit(fmt.Sprintf("Unable to install man page: %e", err), 2)
}
return nil
},
}
return cmd, nil
}
// extractManpageSettings processes the *InstallManpageCommandInput and validates
func extractManpageSettings(opts *InstallManpageCommandInput) (string, string, string, bool, error) {
kim.Printf("passed opts: %# v", opts)
name := opts.AppName
cmdname := opts.CmdName
path := opts.Path
hidden := opts.Hidden
if name == "" {
return "", "", "", hidden, fmt.Errorf("AppName is required. Options passed in: %# v", opts)
}
if path == "" {
path = "/usr/local/share/man/man8"
}
if cmdname == "" {
cmdname = "install-manpage"
}
kim.Printf("name: %s cmdname: %s path: %s hidden: %t", name, cmdname, path, hidden)
return name, cmdname, path, hidden, nil
}
type cliTemplate struct {
App *cli.App
TOC []string
Commands []string
GlobalArgs []string
SynopsisArgs []string
}
func writeDocTemplate(w io.Writer, a *cli.App) error {
const name = "cli"
t, err := template.New(name).Parse(markdownDocTemplate)
if err != nil {
return err
}
toc := generateCommandTree(a.Commands, 0)
return t.ExecuteTemplate(w, name, &cliTemplate{
App: a,
TOC: toc,
Commands: prepareCommands(a.Commands, 0),
GlobalArgs: prepareArgsWithValues(a.VisibleFlags()),
SynopsisArgs: prepareArgsSynopsis(a.VisibleFlags()),
})
}
func generateCommandTree(commands []*cli.Command, level int) []string {
var coms []string
for _, command := range commands {
if command.Hidden {
continue
}
prepared := fmt.Sprintf("%s- [%s](#%s)", strings.Repeat(" ", level), strings.Join(command.Names(), ", "), strings.Join(command.Names(), "-"))
coms = append(coms, prepared)
// recursively iterate subcommands
if len(command.Subcommands) > 0 {
coms = append(
coms,
generateCommandTree(command.Subcommands, level+1)...,
)
}
}
return coms
}
func prepareCommands(commands []*cli.Command, level int) []string {
var coms []string
for _, command := range commands {
if command.Hidden {
continue
}
usageText := prepareUsageText(command)
usage := prepareUsage(command, usageText)
prepared := fmt.Sprintf("%s %s\n\n%s%s",
strings.Repeat("#", level+2),
strings.Join(command.Names(), ", "),
usage,
usageText,
)
flags := prepareArgsWithValues(command.Flags)
if len(flags) > 0 {
prepared += fmt.Sprintf("\n%s", strings.Join(flags, "\n"))
}
coms = append(coms, prepared)
// recursevly iterate subcommands
if len(command.Subcommands) > 0 {
coms = append(
coms,
prepareCommands(command.Subcommands, level+1)...,
)
}
}
return coms
}
func prepareArgsWithValues(flags []cli.Flag) []string {
return prepareFlags(flags, ", ", "**", "**", `""`, true)
}
func prepareArgsSynopsis(flags []cli.Flag) []string {
return prepareFlags(flags, "|", "[", "]", "[value]", false)
}
func prepareFlags(
flags []cli.Flag,
sep, opener, closer, value string,
addDetails bool,
) []string {
args := []string{}
for _, f := range flags {
flag, ok := f.(cli.DocGenerationFlag)
if !ok {
continue
}
modifiedArg := opener
for _, s := range flag.Names() {
trimmed := strings.TrimSpace(s)
if len(modifiedArg) > len(opener) {
modifiedArg += sep
}
if len(trimmed) > 1 {
modifiedArg += fmt.Sprintf("--%s", trimmed)
} else {
modifiedArg += fmt.Sprintf("-%s", trimmed)
}
}
modifiedArg += closer
if flag.TakesValue() {
modifiedArg += fmt.Sprintf("=%s", value)
}
if addDetails {
modifiedArg += flagDetails(flag)
}
args = append(args, modifiedArg+"\n")
}
sort.Strings(args)
return args
}
// flagDetails returns a string containing the flags metadata
func flagDetails(flag cli.DocGenerationFlag) string {
description := flag.GetUsage()
value := flag.GetValue()
if value != "" {
description += " (default: " + value + ")"
}
return ": " + description
}
func prepareUsageText(command *cli.Command) string {
usageText := ""
if command.UsageText != "" {
// Remove leading and trailing newlines
preparedUsageText := strings.TrimSuffix(command.UsageText, "\n")
preparedUsageText = strings.TrimPrefix(preparedUsageText, "\n")
if strings.Contains(preparedUsageText, "\n") {
// Format multi-line string as a code block
usageText = fmt.Sprintf("```\n%s\n```\n", preparedUsageText)
} else {
// Style a single line as a note
usageText = fmt.Sprintf(">%s\n", preparedUsageText)
}
}
return usageText
}
func prepareUsage(command *cli.Command, usageText string) string {
usage := ""
if command.Usage != "" {
usage = fmt.Sprintf("%s\n", command.Usage)
}
// Add a newline to the Usage IFF there is a UsageText
if usageText != "" && usage != "" {
usage = fmt.Sprintf("%s\n", usage)
}
return usage
}