gen_kube_man.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. import (
  15. "bytes"
  16. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "strings"
  20. mangen "github.com/cpuguy83/go-md2man/md2man"
  21. "github.com/spf13/cobra"
  22. "github.com/spf13/pflag"
  23. "k8s.io/kubernetes/cmd/genutils"
  24. apiservapp "k8s.io/kubernetes/cmd/kube-apiserver/app"
  25. cmapp "k8s.io/kubernetes/cmd/kube-controller-manager/app"
  26. proxyapp "k8s.io/kubernetes/cmd/kube-proxy/app"
  27. schapp "k8s.io/kubernetes/cmd/kube-scheduler/app"
  28. kubeadmapp "k8s.io/kubernetes/cmd/kubeadm/app/cmd"
  29. kubeletapp "k8s.io/kubernetes/cmd/kubelet/app"
  30. kubectlcmd "k8s.io/kubernetes/pkg/kubectl/cmd"
  31. )
  32. func main() {
  33. // use os.Args instead of "flags" because "flags" will mess up the man pages!
  34. path := "docs/man/man1"
  35. module := ""
  36. if len(os.Args) == 3 {
  37. path = os.Args[1]
  38. module = os.Args[2]
  39. } else {
  40. fmt.Fprintf(os.Stderr, "usage: %s [output directory] [module] \n", os.Args[0])
  41. os.Exit(1)
  42. }
  43. outDir, err := genutils.OutDir(path)
  44. if err != nil {
  45. fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
  46. os.Exit(1)
  47. }
  48. // Set environment variables used by command so the output is consistent,
  49. // regardless of where we run.
  50. os.Setenv("HOME", "/home/username")
  51. switch module {
  52. case "kube-apiserver":
  53. // generate manpage for kube-apiserver
  54. apiserver := apiservapp.NewAPIServerCommand()
  55. genMarkdown(apiserver, "", outDir)
  56. for _, c := range apiserver.Commands() {
  57. genMarkdown(c, "kube-apiserver", outDir)
  58. }
  59. case "kube-controller-manager":
  60. // generate manpage for kube-controller-manager
  61. controllermanager := cmapp.NewControllerManagerCommand()
  62. genMarkdown(controllermanager, "", outDir)
  63. for _, c := range controllermanager.Commands() {
  64. genMarkdown(c, "kube-controller-manager", outDir)
  65. }
  66. case "kube-proxy":
  67. // generate manpage for kube-proxy
  68. proxy := proxyapp.NewProxyCommand()
  69. genMarkdown(proxy, "", outDir)
  70. for _, c := range proxy.Commands() {
  71. genMarkdown(c, "kube-proxy", outDir)
  72. }
  73. case "kube-scheduler":
  74. // generate manpage for kube-scheduler
  75. scheduler := schapp.NewSchedulerCommand()
  76. genMarkdown(scheduler, "", outDir)
  77. for _, c := range scheduler.Commands() {
  78. genMarkdown(c, "kube-scheduler", outDir)
  79. }
  80. case "kubelet":
  81. // generate manpage for kubelet
  82. kubelet := kubeletapp.NewKubeletCommand()
  83. genMarkdown(kubelet, "", outDir)
  84. for _, c := range kubelet.Commands() {
  85. genMarkdown(c, "kubelet", outDir)
  86. }
  87. case "kubectl":
  88. // generate manpage for kubectl
  89. // TODO os.Stdin should really be something like ioutil.Discard, but a Reader
  90. kubectl := kubectlcmd.NewKubectlCommand(os.Stdin, ioutil.Discard, ioutil.Discard)
  91. genMarkdown(kubectl, "", outDir)
  92. for _, c := range kubectl.Commands() {
  93. genMarkdown(c, "kubectl", outDir)
  94. }
  95. case "kubeadm":
  96. // generate manpage for kubelet
  97. kubeadm := kubeadmapp.NewKubeadmCommand(os.Stdin, os.Stdout, os.Stderr)
  98. genMarkdown(kubeadm, "", outDir)
  99. for _, c := range kubeadm.Commands() {
  100. genMarkdown(c, "kubeadm", outDir)
  101. }
  102. default:
  103. fmt.Fprintf(os.Stderr, "Module %s is not supported", module)
  104. os.Exit(1)
  105. }
  106. }
  107. func preamble(out *bytes.Buffer, name, short, long string) {
  108. out.WriteString(`% KUBERNETES(1) kubernetes User Manuals
  109. % Eric Paris
  110. % Jan 2015
  111. # NAME
  112. `)
  113. fmt.Fprintf(out, "%s \\- %s\n\n", name, short)
  114. fmt.Fprintf(out, "# SYNOPSIS\n")
  115. fmt.Fprintf(out, "**%s** [OPTIONS]\n\n", name)
  116. fmt.Fprintf(out, "# DESCRIPTION\n")
  117. fmt.Fprintf(out, "%s\n\n", long)
  118. }
  119. func printFlags(out *bytes.Buffer, flags *pflag.FlagSet) {
  120. flags.VisitAll(func(flag *pflag.Flag) {
  121. format := "**--%s**=%s\n\t%s\n\n"
  122. if flag.Value.Type() == "string" {
  123. // put quotes on the value
  124. format = "**--%s**=%q\n\t%s\n\n"
  125. }
  126. // Todo, when we mark a shorthand is deprecated, but specify an empty message.
  127. // The flag.ShorthandDeprecated is empty as the shorthand is deprecated.
  128. // Using len(flag.ShorthandDeprecated) > 0 can't handle this, others are ok.
  129. if !(len(flag.ShorthandDeprecated) > 0) && len(flag.Shorthand) > 0 {
  130. format = "**-%s**, " + format
  131. fmt.Fprintf(out, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
  132. } else {
  133. fmt.Fprintf(out, format, flag.Name, flag.DefValue, flag.Usage)
  134. }
  135. })
  136. }
  137. func printOptions(out *bytes.Buffer, command *cobra.Command) {
  138. flags := command.NonInheritedFlags()
  139. if flags.HasFlags() {
  140. fmt.Fprintf(out, "# OPTIONS\n")
  141. printFlags(out, flags)
  142. fmt.Fprintf(out, "\n")
  143. }
  144. flags = command.InheritedFlags()
  145. if flags.HasFlags() {
  146. fmt.Fprintf(out, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")
  147. printFlags(out, flags)
  148. fmt.Fprintf(out, "\n")
  149. }
  150. }
  151. func genMarkdown(command *cobra.Command, parent, docsDir string) {
  152. dparent := strings.Replace(parent, " ", "-", -1)
  153. name := command.Name()
  154. dname := name
  155. if len(parent) > 0 {
  156. dname = dparent + "-" + name
  157. name = parent + " " + name
  158. }
  159. out := new(bytes.Buffer)
  160. short := command.Short
  161. long := command.Long
  162. if len(long) == 0 {
  163. long = short
  164. }
  165. preamble(out, name, short, long)
  166. printOptions(out, command)
  167. if len(command.Example) > 0 {
  168. fmt.Fprintf(out, "# EXAMPLE\n")
  169. fmt.Fprintf(out, "```\n%s\n```\n", command.Example)
  170. }
  171. if len(command.Commands()) > 0 || len(parent) > 0 {
  172. fmt.Fprintf(out, "# SEE ALSO\n")
  173. if len(parent) > 0 {
  174. fmt.Fprintf(out, "**%s(1)**, ", dparent)
  175. }
  176. for _, c := range command.Commands() {
  177. fmt.Fprintf(out, "**%s-%s(1)**, ", dname, c.Name())
  178. genMarkdown(c, name, docsDir)
  179. }
  180. fmt.Fprintf(out, "\n")
  181. }
  182. out.WriteString(`
  183. # HISTORY
  184. January 2015, Originally compiled by Eric Paris (eparis at redhat dot com) based on the kubernetes source material, but hopefully they have been automatically generated since!
  185. `)
  186. final := mangen.Render(out.Bytes())
  187. filename := docsDir + dname + ".1"
  188. outFile, err := os.Create(filename)
  189. if err != nil {
  190. fmt.Println(err)
  191. os.Exit(1)
  192. }
  193. defer outFile.Close()
  194. _, err = outFile.Write(final)
  195. if err != nil {
  196. fmt.Println(err)
  197. os.Exit(1)
  198. }
  199. }