add a flag to disable autogen tag in man and markdown generators

Inherits from parent commands all the way to root
This commit is contained in:
Austin Riendeau 2015-11-07 18:21:25 -07:00 committed by Steve Francia
parent e5762054c9
commit 5144a3aa19
6 changed files with 105 additions and 10 deletions

View File

@ -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) { func TestRunnableRootCommandNilInput(t *testing.T) {
empty_arg := make([]string, 0) empty_arg := make([]string, 0)
c := initializeWithRootCmd() c := initializeWithRootCmd()

View File

@ -93,6 +93,8 @@ type Command struct {
PersistentPostRun func(cmd *Command, args []string) PersistentPostRun func(cmd *Command, args []string)
// PersistentPostRunE: PersistentPostRun but returns an error // PersistentPostRunE: PersistentPostRun but returns an error
PersistentPostRunE func(cmd *Command, args []string) error PersistentPostRunE func(cmd *Command, args []string) error
// DisableAutoGenTag remove
DisableAutoGenTag bool
// Commands is the list of commands supported by this program. // Commands is the list of commands supported by this program.
commands []*Command commands []*Command
// Parent Command for this command // Parent Command for this command
@ -474,15 +476,29 @@ func (c *Command) SuggestionsFor(typedName string) []string {
return suggestions 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 { func (c *Command) Root() *Command {
var findRoot func(*Command) *Command var findRoot func(*Command) *Command
findRoot = func(x *Command) *Command { findRoot = func(x *Command) *Command {
if x.HasParent() { if x.HasParent() {
return findRoot(x.parent) return findRoot(x.parent)
} else {
return x
} }
return x
} }
return findRoot(c) return findRoot(c)

View File

@ -183,20 +183,22 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte {
manPreamble(buf, header, commandName, short, long) manPreamble(buf, header, commandName, short, long)
manPrintOptions(buf, cmd) manPrintOptions(buf, cmd)
if len(cmd.Example) > 0 { if len(cmd.Example) > 0 {
fmt.Fprintf(buf, "# EXAMPLE\n") fmt.Fprintf(buf, "# EXAMPLE\n")
fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example) fmt.Fprintf(buf, "```\n%s\n```\n", cmd.Example)
} }
if cmd.hasSeeAlso() { if cmd.hasSeeAlso() {
fmt.Fprintf(buf, "# SEE ALSO\n") fmt.Fprintf(buf, "# SEE ALSO\n")
if cmd.HasParent() { if cmd.HasParent() {
parentPath := cmd.Parent().CommandPath() parentPath := cmd.Parent().CommandPath()
dashParentPath := strings.Replace(parentPath, " ", "-", -1) dashParentPath := strings.Replace(parentPath, " ", "-", -1)
fmt.Fprintf(buf, "**%s(%s)**, ", dashParentPath, header.Section) fmt.Fprintf(buf, "**%s(%s)**, ", dashParentPath, header.Section)
cmd.VisitParents(func(c *Command) {
if c.DisableAutoGenTag {
cmd.DisableAutoGenTag = c.DisableAutoGenTag
}
})
} }
children := cmd.Commands() children := cmd.Commands()
sort.Sort(byName(children)) sort.Sort(byName(children))
for _, c := range children { for _, c := range children {
@ -207,7 +209,8 @@ func genMarkdown(cmd *Command, header *GenManHeader) []byte {
} }
fmt.Fprintf(buf, "\n") fmt.Fprintf(buf, "\n")
} }
if !cmd.DisableAutoGenTag {
fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006")) fmt.Fprintf(buf, "# HISTORY\n%s Auto generated by spf13/cobra\n", header.Date.Format("2-Jan-2006"))
}
return buf.Bytes() return buf.Bytes()
} }

View File

@ -65,4 +65,30 @@ func TestGenManDoc(t *testing.T) {
unexpected := translate(cmdDeprecated.Name()) unexpected := translate(cmdDeprecated.Name())
checkStringOmits(t, found, unexpected) 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)
} }

View File

@ -82,7 +82,6 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string
} }
printOptions(out, cmd, name) printOptions(out, cmd, name)
if cmd.hasSeeAlso() { if cmd.hasSeeAlso() {
fmt.Fprintf(out, "### SEE ALSO\n") fmt.Fprintf(out, "### SEE ALSO\n")
if cmd.HasParent() { if cmd.HasParent() {
@ -91,6 +90,11 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string
link := pname + ".md" link := pname + ".md"
link = strings.Replace(link, " ", "_", -1) link = strings.Replace(link, " ", "_", -1)
fmt.Fprintf(out, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short) 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() children := cmd.Commands()
@ -107,9 +111,10 @@ func (cmd *Command) GenMarkdownCustom(out *bytes.Buffer, linkHandler func(string
} }
fmt.Fprintf(out, "\n") fmt.Fprintf(out, "\n")
} }
if !cmd.DisableAutoGenTag {
fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006")) fmt.Fprintf(out, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006"))
} }
}
func GenMarkdownTree(cmd *Command, dir string) { func GenMarkdownTree(cmd *Command, dir string) {
cmd.GenMarkdownTree(dir) cmd.GenMarkdownTree(dir)

View File

@ -65,3 +65,20 @@ func TestGenMdDoc(t *testing.T) {
t.Errorf("Unexpected response.\nFound: %v\nBut should not have!!\n", unexpected) 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)
}