md_docs.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //Copyright 2015 Red Hat Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package doc
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io"
  18. "os"
  19. "path/filepath"
  20. "sort"
  21. "strings"
  22. "time"
  23. "github.com/spf13/cobra"
  24. )
  25. func printOptions(buf *bytes.Buffer, cmd *cobra.Command, name string) error {
  26. flags := cmd.NonInheritedFlags()
  27. flags.SetOutput(buf)
  28. if flags.HasFlags() {
  29. buf.WriteString("### Options\n\n```\n")
  30. flags.PrintDefaults()
  31. buf.WriteString("```\n\n")
  32. }
  33. parentFlags := cmd.InheritedFlags()
  34. parentFlags.SetOutput(buf)
  35. if parentFlags.HasFlags() {
  36. buf.WriteString("### Options inherited from parent commands\n\n```\n")
  37. parentFlags.PrintDefaults()
  38. buf.WriteString("```\n\n")
  39. }
  40. return nil
  41. }
  42. // GenMarkdown creates markdown output.
  43. func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
  44. return GenMarkdownCustom(cmd, w, func(s string) string { return s })
  45. }
  46. // GenMarkdownCustom creates custom markdown output.
  47. func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
  48. cmd.InitDefaultHelpCmd()
  49. cmd.InitDefaultHelpFlag()
  50. buf := new(bytes.Buffer)
  51. name := cmd.CommandPath()
  52. short := cmd.Short
  53. long := cmd.Long
  54. if len(long) == 0 {
  55. long = short
  56. }
  57. buf.WriteString("## " + name + "\n\n")
  58. buf.WriteString(short + "\n\n")
  59. buf.WriteString("### Synopsis\n\n")
  60. buf.WriteString(long + "\n\n")
  61. if cmd.Runnable() {
  62. buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.UseLine()))
  63. }
  64. if len(cmd.Example) > 0 {
  65. buf.WriteString("### Examples\n\n")
  66. buf.WriteString(fmt.Sprintf("```\n%s\n```\n\n", cmd.Example))
  67. }
  68. if err := printOptions(buf, cmd, name); err != nil {
  69. return err
  70. }
  71. if hasSeeAlso(cmd) {
  72. buf.WriteString("### SEE ALSO\n\n")
  73. if cmd.HasParent() {
  74. parent := cmd.Parent()
  75. pname := parent.CommandPath()
  76. link := pname + ".md"
  77. link = strings.Replace(link, " ", "_", -1)
  78. buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short))
  79. cmd.VisitParents(func(c *cobra.Command) {
  80. if c.DisableAutoGenTag {
  81. cmd.DisableAutoGenTag = c.DisableAutoGenTag
  82. }
  83. })
  84. }
  85. children := cmd.Commands()
  86. sort.Sort(byName(children))
  87. for _, child := range children {
  88. if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
  89. continue
  90. }
  91. cname := name + " " + child.Name()
  92. link := cname + ".md"
  93. link = strings.Replace(link, " ", "_", -1)
  94. buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))
  95. }
  96. buf.WriteString("\n")
  97. }
  98. if !cmd.DisableAutoGenTag {
  99. buf.WriteString("###### Auto generated by spf13/cobra on " + time.Now().Format("2-Jan-2006") + "\n")
  100. }
  101. _, err := buf.WriteTo(w)
  102. return err
  103. }
  104. // GenMarkdownTree will generate a markdown page for this command and all
  105. // descendants in the directory given. The header may be nil.
  106. // This function may not work correctly if your command names have `-` in them.
  107. // If you have `cmd` with two subcmds, `sub` and `sub-third`,
  108. // and `sub` has a subcommand called `third`, it is undefined which
  109. // help output will be in the file `cmd-sub-third.1`.
  110. func GenMarkdownTree(cmd *cobra.Command, dir string) error {
  111. identity := func(s string) string { return s }
  112. emptyStr := func(s string) string { return "" }
  113. return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)
  114. }
  115. // GenMarkdownTreeCustom is the the same as GenMarkdownTree, but
  116. // with custom filePrepender and linkHandler.
  117. func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
  118. for _, c := range cmd.Commands() {
  119. if !c.IsAvailableCommand() || c.IsAdditionalHelpTopicCommand() {
  120. continue
  121. }
  122. if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
  123. return err
  124. }
  125. }
  126. basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
  127. filename := filepath.Join(dir, basename)
  128. f, err := os.Create(filename)
  129. if err != nil {
  130. return err
  131. }
  132. defer f.Close()
  133. if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
  134. return err
  135. }
  136. if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
  137. return err
  138. }
  139. return nil
  140. }