From ace5fe16e9365b868c354e145b2732da4fac486b Mon Sep 17 00:00:00 2001 From: Mark Scannell Date: Sat, 28 Sep 2019 22:00:54 +0100 Subject: [PATCH] Generating additional help topics when generating markdown --- doc/md_docs.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/doc/md_docs.go b/doc/md_docs.go index d76f6d5e..932da407 100644 --- a/doc/md_docs.go +++ b/doc/md_docs.go @@ -51,11 +51,21 @@ func GenMarkdown(cmd *cobra.Command, w io.Writer) error { } // GenMarkdownCustom creates custom markdown output. -func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error { +func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) (err error) { cmd.InitDefaultHelpCmd() cmd.InitDefaultHelpFlag() buf := new(bytes.Buffer) + defer func() { + if err != nil { + return + } + if !cmd.DisableAutoGenTag { + buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n") + } + _, err = buf.WriteTo(w) + }() + name := cmd.CommandPath() short := cmd.Short @@ -64,6 +74,11 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) long = short } + if cmd.IsAdditionalHelpTopicCommand() { + buf.WriteString(long + "\n\n") + return nil + } + buf.WriteString("## " + name + "\n\n") buf.WriteString(short + "\n\n") buf.WriteString("### Synopsis\n\n") @@ -100,7 +115,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) sort.Sort(byName(children)) for _, child := range children { - if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() { + if !child.IsAvailableCommand() && !child.IsAdditionalHelpTopicCommand() { continue } cname := name + " " + child.Name() @@ -110,11 +125,8 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) } buf.WriteString("\n") } - if !cmd.DisableAutoGenTag { - buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n") - } - _, err := buf.WriteTo(w) - return err + + return nil } // GenMarkdownTree will generate a markdown page for this command and all @@ -133,7 +145,7 @@ func GenMarkdownTree(cmd *cobra.Command, dir string) error { // with custom filePrepender and linkHandler. func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error { for _, c := range cmd.Commands() { - if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() { + if !c.IsAvailableCommand() && !c.IsAdditionalHelpTopicCommand() { continue } if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {