md_docs.go 4.4 KB

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