From 8a505799c632b1e8632c07b92b465e1dfd57d775 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Sat, 31 Jul 2021 00:08:52 +0200 Subject: [PATCH] Add `GenTree` func Signed-off-by: CrazyMax --- docgen.go | 15 +++++++++++++++ docgen_md.go | 9 +++++---- docgen_yaml.go | 11 +++++++---- example/main.go | 13 ++----------- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/docgen.go b/docgen.go index a85042f..4071092 100644 --- a/docgen.go +++ b/docgen.go @@ -1 +1,16 @@ package docgen + +import ( + "github.com/spf13/cobra" +) + +func GenTree(cmd *cobra.Command, dir string) error { + var err error + if err = GenMarkdownTree(cmd, dir); err != nil { + return err + } + if err = GenYamlTree(cmd, dir); err != nil { + return err + } + return nil +} diff --git a/docgen_md.go b/docgen_md.go index e3388f1..afd6e01 100644 --- a/docgen_md.go +++ b/docgen_md.go @@ -15,9 +15,11 @@ import ( "github.com/spf13/pflag" ) -func GenMarkdown(cmd *cobra.Command, dir string) error { +// GenMarkdownTree will generate a markdown page for this command and all +// descendants in the directory given. +func GenMarkdownTree(cmd *cobra.Command, dir string) error { for _, c := range cmd.Commands() { - if err := GenMarkdown(c, dir); err != nil { + if err := GenMarkdownTree(c, dir); err != nil { return err } } @@ -25,11 +27,11 @@ func GenMarkdown(cmd *cobra.Command, dir string) error { return nil } + log.Printf("INFO: Generating Markdown docs for %s", cmd.CommandPath()) mdFile := mdFilename(cmd) fullPath := filepath.Join(dir, mdFile) if _, err := os.Stat(fullPath); os.IsNotExist(err) { - log.Printf("INFO: Init markdown for %s", cmd.CommandPath()) var icBuf bytes.Buffer icTpl, err := template.New("ic").Option("missingkey=error").Parse(`# {{ .Command }} @@ -83,7 +85,6 @@ func GenMarkdown(cmd *cobra.Command, dir string) error { return errors.Wrapf(err, "failed to write %s", fullPath) } - log.Printf("INFO: Markdown updated for %s", cmd.CommandPath()) return nil } diff --git a/docgen_yaml.go b/docgen_yaml.go index c974705..d3a6cea 100644 --- a/docgen_yaml.go +++ b/docgen_yaml.go @@ -62,6 +62,9 @@ type cmdDoc struct { // it is undefined which help output will be in the file `cmd-sub-third.1`. func GenYamlTree(cmd *cobra.Command, dir string) error { emptyStr := func(s string) string { return "" } + if err := loadLongDescription(cmd, dir); err != nil { + return err + } return GenYamlTreeCustom(cmd, dir, emptyStr) } @@ -320,16 +323,16 @@ func hasSeeAlso(cmd *cobra.Command) bool { return false } -// LoadLongDescription gets long descriptions and examples from markdown. -func LoadLongDescription(parentCmd *cobra.Command, path string) error { +// loadLongDescription gets long descriptions and examples from markdown. +func loadLongDescription(parentCmd *cobra.Command, path string) error { for _, cmd := range parentCmd.Commands() { if cmd.HasSubCommands() { - if err := LoadLongDescription(cmd, path); err != nil { + if err := loadLongDescription(cmd, path); err != nil { return err } } name := cmd.CommandPath() - log.Println("INFO: Generating docs for", name) + log.Println("INFO: Generating YAML docs for", name) if i := strings.Index(name, " "); i >= 0 { // remove root command / binary name name = name[i+1:] diff --git a/example/main.go b/example/main.go index 2ba58b0..f258ceb 100644 --- a/example/main.go +++ b/example/main.go @@ -33,19 +33,10 @@ func main() { cwd, _ := os.Getwd() source := filepath.Join(cwd, sourcePath) - if err = os.MkdirAll(sourcePath, 0755); err != nil { + if err = os.MkdirAll(source, 0755); err != nil { log.Printf("ERROR: %+v", err) } - - if err = docgen.GenMarkdown(cmd, sourcePath); err != nil { - log.Printf("ERROR: %+v", err) - } - - if err := docgen.LoadLongDescription(cmd, source); err != nil { - log.Printf("ERROR: %+v", err) - } - - if err = docgen.GenYamlTree(cmd, sourcePath); err != nil { + if err = docgen.GenTree(cmd, source); err != nil { log.Printf("ERROR: %+v", err) } }