git-project-manager/cmd/docs.go

63 lines
1.5 KiB
Go

package cmd
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"gitea.libretechconsulting.com/rmcguire/git-project-manager/cmd/util"
)
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(util.FlagDocsPath)
if err != nil {
utils.Logger().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:
utils.Logger().Error("invalid docs type", utils.Logger().Args("type", args[0]))
}
utils.Logger().Info("docs generation complete", utils.Logger().Args(
"type", args[0], "docsDir", outDir, "err", err))
}
func prepareDocsDir(_ *cobra.Command, outDir string) {
_, err := os.Stat(outDir)
if err != nil {
err = os.Mkdir(outDir, 0o755)
if err != nil {
utils.Logger().Error("failed to create docs path", utils.Logger().Args(
"error", err.Error(), "docsDir", outDir))
}
}
}
func init() {
docsCmd.PersistentFlags().StringP(util.FlagDocsPath, "d", "./docs", "specify output directory for documentation")
rootCmd.AddCommand(docsCmd)
}