md_docs.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "fmt"
  16. "io"
  17. "os"
  18. "path/filepath"
  19. "sort"
  20. "strings"
  21. "time"
  22. "github.com/spf13/cobra"
  23. )
  24. func printOptions(w io.Writer, cmd *cobra.Command, name string) error {
  25. flags := cmd.NonInheritedFlags()
  26. flags.SetOutput(w)
  27. if flags.HasFlags() {
  28. if _, err := fmt.Fprintf(w, "### Options\n\n```\n"); err != nil {
  29. return err
  30. }
  31. flags.PrintDefaults()
  32. if _, err := fmt.Fprintf(w, "```\n\n"); err != nil {
  33. return err
  34. }
  35. }
  36. parentFlags := cmd.InheritedFlags()
  37. parentFlags.SetOutput(w)
  38. if parentFlags.HasFlags() {
  39. if _, err := fmt.Fprintf(w, "### Options inherited from parent commands\n\n```\n"); err != nil {
  40. return err
  41. }
  42. parentFlags.PrintDefaults()
  43. if _, err := fmt.Fprintf(w, "```\n\n"); err != nil {
  44. return err
  45. }
  46. }
  47. return nil
  48. }
  49. func GenMarkdown(cmd *cobra.Command, w io.Writer) error {
  50. return GenMarkdownCustom(cmd, w, func(s string) string { return s })
  51. }
  52. func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) string) error {
  53. name := cmd.CommandPath()
  54. short := cmd.Short
  55. long := cmd.Long
  56. if len(long) == 0 {
  57. long = short
  58. }
  59. if _, err := fmt.Fprintf(w, "## %s\n\n", name); err != nil {
  60. return err
  61. }
  62. if _, err := fmt.Fprintf(w, "%s\n\n", short); err != nil {
  63. return err
  64. }
  65. if _, err := fmt.Fprintf(w, "### Synopsis\n\n"); err != nil {
  66. return err
  67. }
  68. if _, err := fmt.Fprintf(w, "\n%s\n\n", long); err != nil {
  69. return err
  70. }
  71. if cmd.Runnable() {
  72. if _, err := fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.UseLine()); err != nil {
  73. return err
  74. }
  75. }
  76. if len(cmd.Example) > 0 {
  77. if _, err := fmt.Fprintf(w, "### Examples\n\n"); err != nil {
  78. return err
  79. }
  80. if _, err := fmt.Fprintf(w, "```\n%s\n```\n\n", cmd.Example); err != nil {
  81. return err
  82. }
  83. }
  84. if err := printOptions(w, cmd, name); err != nil {
  85. return err
  86. }
  87. if hasSeeAlso(cmd) {
  88. if _, err := fmt.Fprintf(w, "### SEE ALSO\n"); err != nil {
  89. return err
  90. }
  91. if cmd.HasParent() {
  92. parent := cmd.Parent()
  93. pname := parent.CommandPath()
  94. link := pname + ".md"
  95. link = strings.Replace(link, " ", "_", -1)
  96. if _, err := fmt.Fprintf(w, "* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short); err != nil {
  97. return err
  98. }
  99. cmd.VisitParents(func(c *cobra.Command) {
  100. if c.DisableAutoGenTag {
  101. cmd.DisableAutoGenTag = c.DisableAutoGenTag
  102. }
  103. })
  104. }
  105. children := cmd.Commands()
  106. sort.Sort(byName(children))
  107. for _, child := range children {
  108. if !child.IsAvailableCommand() || child.IsHelpCommand() {
  109. continue
  110. }
  111. cname := name + " " + child.Name()
  112. link := cname + ".md"
  113. link = strings.Replace(link, " ", "_", -1)
  114. if _, err := fmt.Fprintf(w, "* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short); err != nil {
  115. return err
  116. }
  117. }
  118. if _, err := fmt.Fprintf(w, "\n"); err != nil {
  119. return err
  120. }
  121. }
  122. if !cmd.DisableAutoGenTag {
  123. if _, err := fmt.Fprintf(w, "###### Auto generated by spf13/cobra on %s\n", time.Now().Format("2-Jan-2006")); err != nil {
  124. return err
  125. }
  126. }
  127. return nil
  128. }
  129. func GenMarkdownTree(cmd *cobra.Command, dir string) error {
  130. identity := func(s string) string { return s }
  131. emptyStr := func(s string) string { return "" }
  132. return GenMarkdownTreeCustom(cmd, dir, emptyStr, identity)
  133. }
  134. func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandler func(string) string) error {
  135. for _, c := range cmd.Commands() {
  136. if !c.IsAvailableCommand() || c.IsHelpCommand() {
  137. continue
  138. }
  139. if err := GenMarkdownTreeCustom(c, dir, filePrepender, linkHandler); err != nil {
  140. return err
  141. }
  142. }
  143. basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md"
  144. filename := filepath.Join(dir, basename)
  145. f, err := os.Create(filename)
  146. if err != nil {
  147. return err
  148. }
  149. defer f.Close()
  150. if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
  151. return err
  152. }
  153. if err := GenMarkdownCustom(cmd, f, linkHandler); err != nil {
  154. return err
  155. }
  156. return nil
  157. }