Skip to content

Commit 8dc7a6a

Browse files
committed
feat(docs): add docs command to generate documentation
1 parent e1173b2 commit 8dc7a6a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

cmd/doc.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/spf13/cobra/doc"
8+
)
9+
10+
const (
11+
flagMkDirs = "mk-dirs"
12+
)
13+
14+
var metaDocsCmd = &cobra.Command{
15+
Use: "docs [dir]",
16+
Short: "Generate documentation to a directory",
17+
Args: cobra.ExactArgs(1),
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
targetPath := args[0]
20+
21+
shouldMkDirs, err := cmd.Flags().GetBool(flagMkDirs)
22+
if err != nil {
23+
return err
24+
}
25+
if shouldMkDirs {
26+
if err := mkDirs(targetPath); err != nil {
27+
return err
28+
}
29+
}
30+
31+
rootCmd.DisableAutoGenTag = true
32+
33+
err = doc.GenMarkdownTree(rootCmd, targetPath)
34+
if err != nil {
35+
return err
36+
}
37+
38+
return nil
39+
},
40+
}
41+
42+
func mkDirs(dir string) error {
43+
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
44+
return err
45+
}
46+
47+
return nil
48+
}
49+
50+
func init() {
51+
metaCmd.AddCommand(metaDocsCmd)
52+
metaDocsCmd.Flags().Bool(flagMkDirs, false, "create directories if they do not exist")
53+
}

cmd/meta.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cmd
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
)
6+
7+
var metaCmd = &cobra.Command{
8+
Use: "meta",
9+
Short: "Meta commands for the tool",
10+
}
11+
12+
func init() {
13+
rootCmd.AddCommand(metaCmd)
14+
}

0 commit comments

Comments
 (0)