Skip to content
Myr

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

6 files+105 added−10 removed

modified:cobra_test.go+28 added
649 unmodified lines
650 t.Errorf("Root Function was not called\n out:%v", x.Error)651 }652}653654func 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 }668669 total = 0670 dsub.VisitParents(add)671 if total != 2 {672 t.Errorf("Should have visited 2 parent but visited %d", total)673 }674675 total = 0676 c.VisitParents(add)677 if total != 0 {678 t.Errorf("Should have not visited any parent but visited %d", total)679 }680}681682func TestRunnableRootCommandNilInput(t *testing.T) {683 empty_arg := make([]string, 0)684 c := initializeWithRootCmd()685
431 unmodified lines
modified:command.go+18 added-2 removed
91 unmodified lines
92 // 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 *Command
373 unmodified lines
475 }476 return suggestions477}478479func (c *Command) VisitParents(fn func(*Command)) {480 var traverse func(*Command) *Command481482 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}493494func (c *Command) Root() *Command {495 var findRoot func(*Command) *Command496497 findRoot = func(x *Command) *Command {498 if x.HasParent() {499 return findRoot(x.parent)483 } else {484 return x500 }501 return x502 }503504 return findRoot(c)505}
691 unmodified lines
modified:man_docs_test.go+26 added
63 unmodified lines
64 checkStringContains(t, found, expected)6566 unexpected := translate(cmdDeprecated.Name())67 checkStringOmits(t, found, unexpected)6869 // auto generated70 expected = translate("Auto generated")71 checkStringContains(t, found, expected)72}7374func TestGenManNoGenTag(t *testing.T) {7576 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)8384 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()9192 unexpected := translate("#HISTORY")93 checkStringOmits(t, found, unexpected)94}
modified:man_docs.go+8 added-5 removed
181 unmodified lines
182 }183184 manPreamble(buf, header, commandName, short, long)185 manPrintOptions(buf, cmd)186186 if len(cmd.Example) > 0 {187 fmt.Fprintf(buf, "# EXAMPLE\n")188 fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)189 }191190 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 }199202 children := cmd.Commands()203 sort.Sort(byName(children))204 for _, c := range children {205 if !c.IsAvailableCommand() || c == cmd.helpCommand {
2 unmodified lines
208 fmt.Fprintf(buf, "**%s-%s(%s)**, ", dashCommandName, c.Name(), header.Section)209 }210 fmt.Fprintf(buf, "\n")211 }210211 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 lines
64 if strings.Contains(found, unexpected) {65 t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected)66 }67}6869func 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)7778 GenMarkdown(c, out)79 found := out.String()8081 unexpected := "Auto generated"82 checkStringOmits(t, found, unexpected)8384}
modified:md_docs.go+8 added-3 removed
80 unmodified lines
81 fmt.Fprintf(out, "```\n%s\n```\n\n", cmd.Example)82 }8384 printOptions(out, cmd, name)8585 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 }99100 children := cmd.Commands()101 sort.Sort(byName(children))
8 unmodified lines
110 fmt.Fprintf(out, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)111 }112 fmt.Fprintf(out, "\n")113 }110111 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}118119func GenMarkdownTree(cmd *Command, dir string) {120 cmd.GenMarkdownTree(dir)
42 unmodified lines