From cbd1914f283c19339ef12d351e9118f510e7a492 Mon Sep 17 00:00:00 2001 From: Austin Riendeau Date: Sat, 7 Nov 2015 18:21:25 -0700 Subject: [PATCH] adds a flag to disable autogen tag at the bottom of man and markdown generators. Inherits from parent commands all the way to root --- cobra_test.go | 28 ++++++++++++++++++++++++++++ command.go | 20 ++++++++++++++++++-- man_docs.go | 13 ++++++++----- man_docs_test.go | 26 ++++++++++++++++++++++++++ md_docs.go | 11 ++++++++--- md_docs_test.go | 17 +++++++++++++++++ 6 files changed, 105 insertions(+), 10 deletions(-) diff --git a/cobra_test.go b/cobra_test.go index 17361c32..9a15fdb5 100644 --- a/cobra_test.go +++ b/cobra_test.go @@ -651,6 +651,34 @@ func TestRunnableRootCommand(t *testing.T) { } } +func TestVisitParents(t *testing.T) { + c := &Command{Use: "app"} + sub := &Command{Use: "sub"} + dsub := &Command{Use: "dsub"} + sub.AddCommand(dsub) + c.AddCommand(sub) + total := 0 + add := func(x *Command) { + total++ + } + sub.VisitParents(add) + if total != 1 { + t.Errorf("Should have visited 1 parent but visited %d", total) + } + + total = 0 + dsub.VisitParents(add) + if total != 2 { + t.Errorf("Should have visited 2 parent but visited %d", total) + } + + total = 0 + c.VisitParents(add) + if total != 0 { + t.Errorf("Should have not visited any parent but visited %d", total) + } +} + func TestRunnableRootCommandNilInput(t *testing.T) { empty_arg := make([]string, 0) c := initializeWithRootCmd() diff --git a/command.go b/command.go index 558fed45..39139508 100644 --- a/command.go +++ b/command.go @@ -92,6 +92,8 @@ type Command struct { PersistentPostRun func(cmd *Command, args []string) // PersistentPostRunE: PersistentPostRun but returns an error PersistentPostRunE func(cmd *Command, args []string) error + // DisableAutoGenTag remove + DisableAutoGenTag bool // Commands is the list of commands supported by this program. commands []*Command // Parent Command for this command @@ -473,15 +475,29 @@ func (c *Command) SuggestionsFor(typedName string) []string { return suggestions } +func (c *Command) VisitParents(fn func(*Command)) { + var traverse func(*Command) *Command + + traverse = func(x *Command) *Command { + if x != c { + fn(x) + } + if x.HasParent() { + return traverse(x.parent) + } + return x + } + traverse(c) +} + func (c *Command) Root() *Command { var findRoot func(*Command) *Command findRoot = func(x *Command) *Command { if x.HasParent() { return findRoot(x.parent) - } else { - return x } + return x } return findRoot(c) diff --git a/man_docs.go b/man_docs.go index e61fff18..43fd2a53 100644 --- a/man_docs.go +++ b/man_docs.go @@ -183,20 +183,22 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte { manPreamble(buf, header, commandName, short, long) manPrintOptions(buf, cmd) - if len(cmd.Example) > 0 { fmt.Fprintf(buf, "# EXAMPLE\n") fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example) } - if cmd.hasSeeAlso() { fmt.Fprintf(buf, "# SEE ALSO\n") if cmd.HasParent() { parentPath := cmd.Parent().CommandPath() dashParentPath := strings.Replace(parentPath, " ", "-", -1) fmt.Fprintf(buf, "**%s(%s)**, ", dashParentPath, header.Section) + cmd.VisitParents(func(c *Command) { + if c.DisableAutoGenTag { + cmd.DisableAutoGenTag = c.DisableAutoGenTag + } + }) } - children := cmd.Commands() sort.Sort(byName(children)) for _, c := range children { @@ -207,7 +209,8 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte { } fmt.Fprintf(buf, "\n") } - - fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006")) + if !cmd.DisableAutoGenTag { + fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006")) + } return buf.Bytes() } diff --git a/man_docs_test.go b/man_docs_test.go index 1c121d6c..ab4030c3 100644 --- a/man_docs_test.go +++ b/man_docs_test.go @@ -65,4 +65,30 @@ func TestGenManDoc(t *testing.T) { unexpected := translate(cmdDeprecated.Name()) checkStringOmits(t, found, unexpected) + + // auto generated + expected = translate("Auto generated") + checkStringContains(t, found, expected) +} + +func TestGenManNoGenTag(t *testing.T) { + + c := initializeWithRootCmd() + // Need two commands to run the command alphabetical sort + cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated) + c.AddCommand(cmdPrint, cmdEcho) + cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp) + cmdEcho.DisableAutoGenTag = true + out := new(bytes.Buffer) + + header := &GenManHeader{ + Title: "Project", + Section: "2", + } + // We generate on a subcommand so we have both subcommands and parents + cmdEcho.GenMan(header, out) + found := out.String() + + unexpected := translate("#HISTORY") + checkStringOmits(t, found, unexpected) } diff --git a/md_docs.go b/md_docs.go index 3c27f7ae..e940ba16 100644 --- a/md_docs.go +++ b/md_docs.go @@ -82,7 +82,6 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string } printOptions(out, cmd, name) - if cmd.hasSeeAlso() { fmt.Fprintf(out, "### SEE ALSO\n") if cmd.HasParent() { @@ -91,6 +90,11 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string link := pname + ".md" link = strings.Replace(link, " ", "_", -1) fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short) + cmd.VisitParents(func(c *Command) { + if c.DisableAutoGenTag { + cmd.DisableAutoGenTag = c.DisableAutoGenTag + } + }) } children := cmd.Commands() @@ -107,8 +111,9 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string } fmt.Fprintf(out, "\n") } - - fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006")) + if !cmd.DisableAutoGenTag { + fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006")) + } } func GenMarkdownTree(cmd *Command, dir string) { diff --git a/md_docs_test.go b/md_docs_test.go index defc9411..82f5452c 100644 --- a/md_docs_test.go +++ b/md_docs_test.go @@ -65,3 +65,20 @@ func TestGenMdDoc(t *testing.T) { t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected) } } + +func TestGenMdNoTag(t *testing.T) { + c := initializeWithRootCmd() + // Need two commands to run the command alphabetical sort + cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated) + c.AddCommand(cmdPrint, cmdEcho) + c.DisableAutoGenTag = true + cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp) + out := new(bytes.Buffer) + + GenMarkdown(c, out) + found := out.String() + + unexpected := "Auto generated" + checkStringOmits(t, found, unexpected) + +}