gen_kubectl_yaml.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. "github.com/spf13/cobra"
  21. "github.com/spf13/pflag"
  22. "gopkg.in/yaml.v2"
  23. "k8s.io/kubernetes/cmd/genutils"
  24. "k8s.io/kubernetes/pkg/kubectl/cmd"
  25. )
  26. type cmdOption struct {
  27. Name string
  28. Shorthand string `yaml:",omitempty"`
  29. DefaultValue string `yaml:"default_value,omitempty"`
  30. Usage string `yaml:",omitempty"`
  31. }
  32. type cmdDoc struct {
  33. Name string
  34. Synopsis string `yaml:",omitempty"`
  35. Description string `yaml:",omitempty"`
  36. Options []cmdOption `yaml:",omitempty"`
  37. InheritedOptions []cmdOption `yaml:"inherited_options,omitempty"`
  38. Example string `yaml:",omitempty"`
  39. SeeAlso []string `yaml:"see_also,omitempty"`
  40. }
  41. func main() {
  42. path := "docs/yaml/kubectl"
  43. if len(os.Args) == 2 {
  44. path = os.Args[1]
  45. } else if len(os.Args) > 2 {
  46. fmt.Fprintf(os.Stderr, "usage: %s [output directory]\n", os.Args[0])
  47. os.Exit(1)
  48. }
  49. outDir, err := genutils.OutDir(path)
  50. if err != nil {
  51. fmt.Fprintf(os.Stderr, "failed to get output directory: %v\n", err)
  52. os.Exit(1)
  53. }
  54. // Set environment variables used by kubectl so the output is consistent,
  55. // regardless of where we run.
  56. os.Setenv("HOME", "/home/username")
  57. kubectl := cmd.NewKubectlCommand(bytes.NewReader(nil), ioutil.Discard, ioutil.Discard)
  58. genYaml(kubectl, "", outDir)
  59. for _, c := range kubectl.Commands() {
  60. genYaml(c, "kubectl", outDir)
  61. }
  62. }
  63. // Temporary workaround for yaml lib generating incorrect yaml with long strings
  64. // that do not contain \n.
  65. func forceMultiLine(s string) string {
  66. if len(s) > 60 && !strings.Contains(s, "\n") {
  67. s = s + "\n"
  68. }
  69. return s
  70. }
  71. func genFlagResult(flags *pflag.FlagSet) []cmdOption {
  72. result := []cmdOption{}
  73. flags.VisitAll(func(flag *pflag.Flag) {
  74. // Todo, when we mark a shorthand is deprecated, but specify an empty message.
  75. // The flag.ShorthandDeprecated is empty as the shorthand is deprecated.
  76. // Using len(flag.ShorthandDeprecated) > 0 can't handle this, others are ok.
  77. if !(len(flag.ShorthandDeprecated) > 0) && len(flag.Shorthand) > 0 {
  78. opt := cmdOption{
  79. flag.Name,
  80. flag.Shorthand,
  81. flag.DefValue,
  82. forceMultiLine(flag.Usage),
  83. }
  84. result = append(result, opt)
  85. } else {
  86. opt := cmdOption{
  87. Name: flag.Name,
  88. DefaultValue: forceMultiLine(flag.DefValue),
  89. Usage: forceMultiLine(flag.Usage),
  90. }
  91. result = append(result, opt)
  92. }
  93. })
  94. return result
  95. }
  96. func genYaml(command *cobra.Command, parent, docsDir string) {
  97. doc := cmdDoc{}
  98. doc.Name = command.Name()
  99. doc.Synopsis = forceMultiLine(command.Short)
  100. doc.Description = forceMultiLine(command.Long)
  101. flags := command.NonInheritedFlags()
  102. if flags.HasFlags() {
  103. doc.Options = genFlagResult(flags)
  104. }
  105. flags = command.InheritedFlags()
  106. if flags.HasFlags() {
  107. doc.InheritedOptions = genFlagResult(flags)
  108. }
  109. if len(command.Example) > 0 {
  110. doc.Example = command.Example
  111. }
  112. if len(command.Commands()) > 0 || len(parent) > 0 {
  113. result := []string{}
  114. if len(parent) > 0 {
  115. result = append(result, parent)
  116. }
  117. for _, c := range command.Commands() {
  118. result = append(result, c.Name())
  119. }
  120. doc.SeeAlso = result
  121. }
  122. final, err := yaml.Marshal(&doc)
  123. if err != nil {
  124. fmt.Println(err)
  125. os.Exit(1)
  126. }
  127. var filename string
  128. if parent == "" {
  129. filename = docsDir + doc.Name + ".yaml"
  130. } else {
  131. filename = docsDir + parent + "_" + doc.Name + ".yaml"
  132. }
  133. outFile, err := os.Create(filename)
  134. if err != nil {
  135. fmt.Println(err)
  136. os.Exit(1)
  137. }
  138. defer outFile.Close()
  139. _, err = outFile.Write(final)
  140. if err != nil {
  141. fmt.Println(err)
  142. os.Exit(1)
  143. }
  144. }