-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
generate.go
79 lines (65 loc) · 1.55 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
package clidoc
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Generate generates markdown documentation for a cobra command and its children.
func Generate(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("command expects one argument which is the path to the output directory")
}
return generate(cmd, args[0])
}
func trimExt(s string) string {
return strings.ReplaceAll(strings.TrimSuffix(s, filepath.Ext(s)), "_", "-")
}
func generate(cmd *cobra.Command, dir string) error {
cmd.DisableAutoGenTag = true
for _, c := range cmd.Commands() {
if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
continue
}
if err := generate(c, dir); err != nil {
return err
}
}
basename := strings.Replace(cmd.CommandPath(), " ", "-", -1)
if err := os.MkdirAll(filepath.Join(dir), 0750); err != nil {
return err
}
filename := filepath.Join(dir, basename) + ".md"
f, err := os.Create(filename) //#nosec:G304
if err != nil {
return err
}
defer (func() { _ = f.Close() })()
if _, err := io.WriteString(f, fmt.Sprintf(`---
id: %s
title: %s
description: %s %s
---
<!--
This file is auto-generated.
To improve this file please make your change against the appropriate "./cmd/*.go" file.
-->
`,
basename,
cmd.CommandPath(),
cmd.CommandPath(),
cmd.Short,
)); err != nil {
return err
}
var b bytes.Buffer
if err := GenMarkdownCustom(cmd, &b, trimExt); err != nil {
return err
}
_, err = f.WriteString(b.String())
return err
}