gen_kubectl_man.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "k8s.io/kubernetes/pkg/kubectl/cmd"
  25. cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
  26. )
  27. func main() {
  28. // use os.Args instead of "flags" because "flags" will mess up the man pages!
  29. path := "docs/man/man1"
  30. if len(os.Args) == 2 {
  31. path = os.Args[1]
  32. } else if len(os.Args) > 2 {
  33. fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0])
  34. os.Exit(1)
  35. }
  36. outDir, err := genutils.OutDir(path)
  37. if err != nil {
  38. fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
  39. os.Exit(1)
  40. }
  41. // Set environment variables used by kubectl so the output is consistent,
  42. // regardless of where we run.
  43. os.Setenv("HOME", "/home/username")
  44. // TODO os.Stdin should really be something like ioutil.Discard, but a Reader
  45. kubectl := cmd.NewKubectlCommand(cmdutil.NewFactory(nil), os.Stdin, ioutil.Discard, ioutil.Discard)
  46. genMarkdown(kubectl, "", outDir)
  47. for _, c := range kubectl.Commands() {
  48. genMarkdown(c, "kubectl", outDir)
  49. }
  50. }
  51. func preamble(out *bytes.Buffer, name, short, long string) {
  52. out.WriteString(`% KUBERNETES(1) kubernetes User Manuals
  53. % Eric Paris
  54. % Jan 2015
  55. # NAME
  56. `)
  57. fmt.Fprintf(out, "%s \\- %s\n\n", name, short)
  58. fmt.Fprintf(out, "# SYNOPSIS\n")
  59. fmt.Fprintf(out, "**%s** [OPTIONS]\n\n", name)
  60. fmt.Fprintf(out, "# DESCRIPTION\n")
  61. fmt.Fprintf(out, "%s\n\n", long)
  62. }
  63. func printFlags(out *bytes.Buffer, flags *pflag.FlagSet) {
  64. flags.VisitAll(func(flag *pflag.Flag) {
  65. format := "**--%s**=%s\n\t%s\n\n"
  66. if flag.Value.Type() == "string" {
  67. // put quotes on the value
  68. format = "**--%s**=%q\n\t%s\n\n"
  69. }
  70. // Todo, when we mark a shorthand is deprecated, but specify an empty message.
  71. // The flag.ShorthandDeprecated is empty as the shorthand is deprecated.
  72. // Using len(flag.ShorthandDeprecated) > 0 can't handle this, others are ok.
  73. if !(len(flag.ShorthandDeprecated) > 0) && len(flag.Shorthand) > 0 {
  74. format = "**-%s**, " + format
  75. fmt.Fprintf(out, format, flag.Shorthand, flag.Name, flag.DefValue, flag.Usage)
  76. } else {
  77. fmt.Fprintf(out, format, flag.Name, flag.DefValue, flag.Usage)
  78. }
  79. })
  80. }
  81. func printOptions(out *bytes.Buffer, command *cobra.Command) {
  82. flags := command.NonInheritedFlags()
  83. if flags.HasFlags() {
  84. fmt.Fprintf(out, "# OPTIONS\n")
  85. printFlags(out, flags)
  86. fmt.Fprintf(out, "\n")
  87. }
  88. flags = command.InheritedFlags()
  89. if flags.HasFlags() {
  90. fmt.Fprintf(out, "# OPTIONS INHERITED FROM PARENT COMMANDS\n")
  91. printFlags(out, flags)
  92. fmt.Fprintf(out, "\n")
  93. }
  94. }
  95. func genMarkdown(command *cobra.Command, parent, docsDir string) {
  96. dparent := strings.Replace(parent, " ", "-", -1)
  97. name := command.Name()
  98. dname := name
  99. if len(parent) > 0 {
  100. dname = dparent + "-" + name
  101. name = parent + " " + name
  102. }
  103. out := new(bytes.Buffer)
  104. short := command.Short
  105. long := command.Long
  106. if len(long) == 0 {
  107. long = short
  108. }
  109. preamble(out, name, short, long)
  110. printOptions(out, command)
  111. if len(command.Example) > 0 {
  112. fmt.Fprintf(out, "# EXAMPLE\n")
  113. fmt.Fprintf(out, "```\n%s\n```\n", command.Example)
  114. }
  115. if len(command.Commands()) > 0 || len(parent) > 0 {
  116. fmt.Fprintf(out, "# SEE ALSO\n")
  117. if len(parent) > 0 {
  118. fmt.Fprintf(out, "**%s(1)**, ", dparent)
  119. }
  120. for _, c := range command.Commands() {
  121. fmt.Fprintf(out, "**%s-%s(1)**, ", dname, c.Name())
  122. genMarkdown(c, name, docsDir)
  123. }
  124. fmt.Fprintf(out, "\n")
  125. }
  126. out.WriteString(`
  127. # HISTORY
  128. 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!
  129. `)
  130. final := mangen.Render(out.Bytes())
  131. filename := docsDir + dname + ".1"
  132. outFile, err := os.Create(filename)
  133. if err != nil {
  134. fmt.Println(err)
  135. os.Exit(1)
  136. }
  137. defer outFile.Close()
  138. _, err = outFile.Write(final)
  139. if err != nil {
  140. fmt.Println(err)
  141. os.Exit(1)
  142. }
  143. }