61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/cobra/doc"
|
|
)
|
|
|
|
var docsCmd = &cobra.Command{
|
|
Use: "docs",
|
|
Aliases: []string{"documentation"},
|
|
Short: "Generate documentation for git-project-manager",
|
|
Args: cobra.RangeArgs(1, 1),
|
|
ValidArgs: []string{"md", "man", "yaml"},
|
|
Run: runDocsCmd,
|
|
}
|
|
|
|
func runDocsCmd(cmd *cobra.Command, args []string) {
|
|
outDir, err := cmd.Flags().GetString(FlagDocsPath)
|
|
if err != nil {
|
|
plog.Error("missing docs path")
|
|
}
|
|
|
|
prepareDocsDir(cmd, outDir)
|
|
|
|
switch args[0] {
|
|
case "md":
|
|
err = doc.GenMarkdownTree(cmd.Root(), outDir)
|
|
case "man":
|
|
err = doc.GenManTree(cmd.Root(), &doc.GenManHeader{
|
|
Title: "EIA Client",
|
|
Section: "1",
|
|
}, outDir)
|
|
case "yaml":
|
|
err = doc.GenYamlTree(cmd.Root(), outDir)
|
|
default:
|
|
plog.Error("invalid docs type", plog.Args("type", args[0]))
|
|
}
|
|
|
|
plog.Info("docs generation complete", plog.Args(
|
|
"type", args[0], "docsDir", outDir))
|
|
}
|
|
|
|
func prepareDocsDir(cmd *cobra.Command, outDir string) {
|
|
_, err := os.Stat(outDir)
|
|
if err != nil {
|
|
err = os.Mkdir(outDir, 0o755)
|
|
if err != nil {
|
|
plog.Error("failed to create docs path", plog.Args(
|
|
"error", err.Error(), "docsDir", outDir))
|
|
}
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
docsCmd.PersistentFlags().StringP(FlagDocsPath, "d", "./docs", "specify output directory for documentation")
|
|
|
|
rootCmd.AddCommand(docsCmd)
|
|
}
|