git-project-manager/cmd/docs.go

61 lines
1.4 KiB
Go
Raw Normal View History

2024-12-19 19:51:26 +00:00
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(
2024-12-27 22:42:44 +00:00
"type", args[0], "docsDir", outDir, "err", err))
2024-12-19 19:51:26 +00:00
}
2024-12-27 22:42:44 +00:00
func prepareDocsDir(_ *cobra.Command, outDir string) {
2024-12-19 19:51:26 +00:00
_, 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)
}