-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
80 lines (64 loc) · 1.63 KB
/
gen.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
package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-faster/errors"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/iyear/tdl/pkg/utils"
)
func NewGen() *cobra.Command {
cmd := &cobra.Command{
Use: "gen",
Short: "A set of gen tools",
Hidden: true,
}
cmd.AddCommand(NewGenDoc())
return cmd
}
func NewGenDoc() *cobra.Command {
var dir string
cmd := &cobra.Command{
Use: "doc",
Short: "Generate doc",
RunE: func(cmd *cobra.Command, args []string) error {
const frontmatter = `---
title: "%s"
bookHidden: true
---
`
cmd.VisitParents(func(c *cobra.Command) {
// Disable the "Auto generated by spf13/cobra on DATE"
// as it creates a lot of diffs.
c.DisableAutoGenTag = true
})
if !utils.FS.PathExists(dir) {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return errors.Wrap(err, "mkdir")
}
}
prepender := func(filename string) string {
name := filepath.Base(filename)
base := strings.TrimSuffix(name, path.Ext(name))
return fmt.Sprintf(frontmatter, strings.Replace(base, "_", " ", -1))
}
linkHandler := func(name string) string {
base := strings.TrimSuffix(name, path.Ext(name))
return "/more/cli/" + strings.ToLower(base) + "/"
}
fmt.Println("Generating command-line documentation in", dir, "...")
err := doc.GenMarkdownTreeCustom(cmd.Root(), dir, prepender, linkHandler)
if err != nil {
return errors.Wrap(err, "gendoc")
}
fmt.Println("Done.")
return nil
},
}
cmd.Flags().StringVarP(&dir, "dir", "d", "", "dir to generate doc")
_ = cmd.MarkFlagRequired("dir")
return cmd
}