add a flag to disable autogen tag in man and markdown generators
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Inherits from parent commands all the way to root
authored by Austin Riendeau · committed by Steve Francia · parent e576205 · Browse files · 6 files·+105 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+105 added−10 removed
- other:+105 added−10 removed
Provenance
- Commit
5144a3a- Tree
d86674d- Author
Austin Riendeau <austin@apriendeau.com>- Committer
Steve Francia <steve.francia@gmail.com>- Parent
e576205
6 files changed+105 added−10 removed
modified:cobra_test.go+28 added
649 unmodified lines650 t.Errorf("Root Function was not called\n out:%v", x.Error)651 }652 }653 654+func TestVisitParents(t *testing.T) {655+ c := &Command{Use: "app"}656+ sub := &Command{Use: "sub"}657+ dsub := &Command{Use: "dsub"}658+ sub.AddCommand(dsub)659+ c.AddCommand(sub)660+ total := 0661+ add := func(x *Command) {662+ total++663+ }664+ sub.VisitParents(add)665+ if total != 1 {666+ t.Errorf("Should have visited 1 parent but visited %d", total)667+ }668+669+ total = 0670+ dsub.VisitParents(add)671+ if total != 2 {672+ t.Errorf("Should have visited 2 parent but visited %d", total)673+ }674+675+ total = 0676+ c.VisitParents(add)677+ if total != 0 {678+ t.Errorf("Should have not visited any parent but visited %d", total)679+ }680+}681+682 func TestRunnableRootCommandNilInput(t *testing.T) {683 empty_arg := make([]string, 0)684 c := initializeWithRootCmd()685 431 unmodified linesmodified:command.go+18 added-2 removed
91 unmodified lines92 // PersistentPostRun: children of this command will inherit and execute after PostRun93 PersistentPostRun func(cmd *Command, args []string)94 // PersistentPostRunE: PersistentPostRun but returns an error95 PersistentPostRunE func(cmd *Command, args []string) error96+ // DisableAutoGenTag remove97+ DisableAutoGenTag bool98 // Commands is the list of commands supported by this program.99 commands []*Command100 // Parent Command for this command101 parent *Command373 unmodified lines475 }476 return suggestions477 }478 479+func (c *Command) VisitParents(fn func(*Command)) {480+ var traverse func(*Command) *Command481+482+ traverse = func(x *Command) *Command {483+ if x != c {484+ fn(x)485+ }486+ if x.HasParent() {487+ return traverse(x.parent)488+ }489+ return x490+ }491+ traverse(c)492+}493+494 func (c *Command) Root() *Command {495 var findRoot func(*Command) *Command496 497 findRoot = func(x *Command) *Command {498 if x.HasParent() {499 return findRoot(x.parent)483- } else {484- return x500 }501+ return x502 }503 504 return findRoot(c)505 }691 unmodified linesmodified:man_docs_test.go+26 added
63 unmodified lines64 checkStringContains(t, found, expected)65 66 unexpected := translate(cmdDeprecated.Name())67 checkStringOmits(t, found, unexpected)68+69+ // auto generated70+ expected = translate("Auto generated")71+ checkStringContains(t, found, expected)72 }73+74+func TestGenManNoGenTag(t *testing.T) {75+76+ c := initializeWithRootCmd()77+ // Need two commands to run the command alphabetical sort78+ cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)79+ c.AddCommand(cmdPrint, cmdEcho)80+ cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)81+ cmdEcho.DisableAutoGenTag = true82+ out := new(bytes.Buffer)83+84+ header := &GenManHeader{85+ Title: "Project",86+ Section: "2",87+ }88+ // We generate on a subcommand so we have both subcommands and parents89+ cmdEcho.GenMan(header, out)90+ found := out.String()91+92+ unexpected := translate("#HISTORY")93+ checkStringOmits(t, found, unexpected)94+}modified:man_docs.go+8 added-5 removed
181 unmodified lines182 }183 184 manPreamble(buf, header, commandName, short, long)185 manPrintOptions(buf, cmd)186-186 if len(cmd.Example) > 0 {187 fmt.Fprintf(buf, "# EXAMPLE\n")188 fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)189 }191-190 if cmd.hasSeeAlso() {191 fmt.Fprintf(buf, "# SEE ALSO\n")192 if cmd.HasParent() {193 parentPath := cmd.Parent().CommandPath()194 dashParentPath := strings.Replace(parentPath, " ", "-", -1)195 fmt.Fprintf(buf, "**%s(%s)**, ", dashParentPath, header.Section)196+ cmd.VisitParents(func(c *Command) {197+ if c.DisableAutoGenTag {198+ cmd.DisableAutoGenTag = c.DisableAutoGenTag199+ }200+ })201 }199-202 children := cmd.Commands()203 sort.Sort(byName(children))204 for _, c := range children {205 if !c.IsAvailableCommand() || c == cmd.helpCommand {2 unmodified lines208 fmt.Fprintf(buf, "**%s-%s(%s)**, ", dashCommandName, c.Name(), header.Section)209 }210 fmt.Fprintf(buf, "\n")211 }210-211- fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))212+ if !cmd.DisableAutoGenTag {213+ fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))214+ }215 return buf.Bytes()216 }modified:md_docs_test.go+17 added
63 unmodified lines64 if strings.Contains(found, unexpected) {65 t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)66 }67 }68+69+func TestGenMdNoTag(t *testing.T) {70+ c := initializeWithRootCmd()71+ // Need two commands to run the command alphabetical sort72+ cmdEcho.AddCommand(cmdTimes, cmdEchoSub, cmdDeprecated)73+ c.AddCommand(cmdPrint, cmdEcho)74+ c.DisableAutoGenTag = true75+ cmdRootWithRun.PersistentFlags().StringVarP(&flags2a, "rootflag", "r", "two", strtwoParentHelp)76+ out := new(bytes.Buffer)77+78+ GenMarkdown(c, out)79+ found := out.String()80+81+ unexpected := "Auto generated"82+ checkStringOmits(t, found, unexpected)83+84+}modified:md_docs.go+8 added-3 removed
80 unmodified lines81 fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.Example)82 }83 84 printOptions(out, cmd, name)85-85 if cmd.hasSeeAlso() {86 fmt.Fprintf(out, "### SEE ALSO\n")87 if cmd.HasParent() {88 parent := cmd.Parent()89 pname := parent.CommandPath()90 link := pname + ".md"91 link = strings.Replace(link, " ", "_", -1)92 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)93+ cmd.VisitParents(func(c *Command) {94+ if c.DisableAutoGenTag {95+ cmd.DisableAutoGenTag = c.DisableAutoGenTag96+ }97+ })98 }99 100 children := cmd.Commands()101 sort.Sort(byName(children))8 unmodified lines110 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)111 }112 fmt.Fprintf(out, "\n")113 }110-111- fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))114+ if !cmd.DisableAutoGenTag {115+ fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))116+ }117 }118 119 func GenMarkdownTree(cmd *Command, dir string) {120 cmd.GenMarkdownTree(dir)42 unmodified lines