cmd_sanity.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright 2016 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 sanity
  14. import (
  15. "fmt"
  16. "os"
  17. "regexp"
  18. "strings"
  19. "github.com/spf13/cobra"
  20. "github.com/spf13/pflag"
  21. "k8s.io/kubernetes/pkg/kubectl/util/templates"
  22. )
  23. // CmdCheck is the commom type of functions to check cobra commands
  24. type CmdCheck func(cmd *cobra.Command) []error
  25. // GlobalCheck is the common type of functions to check global flags
  26. type GlobalCheck func() []error
  27. var (
  28. // AllCmdChecks is the list of CmdCheck type functions
  29. AllCmdChecks = []CmdCheck{
  30. CheckLongDesc,
  31. CheckExamples,
  32. CheckFlags,
  33. }
  34. // AllGlobalChecks is the list of GlobalCheck type functions
  35. AllGlobalChecks = []GlobalCheck{
  36. CheckGlobalVarFlags,
  37. }
  38. )
  39. // RunGlobalChecks runs all the GlobalCheck functions passed and checks for error
  40. func RunGlobalChecks(globalChecks []GlobalCheck) []error {
  41. fmt.Fprint(os.Stdout, "---+ RUNNING GLOBAL CHECKS\n")
  42. errors := []error{}
  43. for _, check := range globalChecks {
  44. errors = append(errors, check()...)
  45. }
  46. return errors
  47. }
  48. // RunCmdChecks runs all the CmdCheck functions passed, skipping skippable commands and looks for error
  49. func RunCmdChecks(cmd *cobra.Command, cmdChecks []CmdCheck, skipCmd []string) []error {
  50. cmdPath := cmd.CommandPath()
  51. for _, skipCmdPath := range skipCmd {
  52. if cmdPath == skipCmdPath {
  53. fmt.Fprintf(os.Stdout, "---+ skipping command %s\n", cmdPath)
  54. return []error{}
  55. }
  56. }
  57. errors := []error{}
  58. if cmd.HasSubCommands() {
  59. for _, subCmd := range cmd.Commands() {
  60. errors = append(errors, RunCmdChecks(subCmd, cmdChecks, skipCmd)...)
  61. }
  62. }
  63. fmt.Fprintf(os.Stdout, "---+ RUNNING COMMAND CHECKS on %q\n", cmdPath)
  64. for _, check := range cmdChecks {
  65. if err := check(cmd); err != nil && len(err) > 0 {
  66. errors = append(errors, err...)
  67. }
  68. }
  69. return errors
  70. }
  71. // CheckLongDesc checks if the long description is valid
  72. func CheckLongDesc(cmd *cobra.Command) []error {
  73. fmt.Fprint(os.Stdout, " ↳ checking long description\n")
  74. cmdPath := cmd.CommandPath()
  75. long := cmd.Long
  76. if len(long) > 0 {
  77. if strings.Trim(long, " \t\n") != long {
  78. return []error{fmt.Errorf(`command %q: long description is not normalized, make sure you are calling templates.LongDesc (from pkg/cmd/templates) before assigning cmd.Long`, cmdPath)}
  79. }
  80. }
  81. return nil
  82. }
  83. // CheckExamples checks if the command examples are valid
  84. func CheckExamples(cmd *cobra.Command) []error {
  85. fmt.Fprint(os.Stdout, " ↳ checking examples\n")
  86. cmdPath := cmd.CommandPath()
  87. examples := cmd.Example
  88. errors := []error{}
  89. if len(examples) > 0 {
  90. for _, line := range strings.Split(examples, "\n") {
  91. if !strings.HasPrefix(line, templates.Indentation) {
  92. errors = append(errors, fmt.Errorf(`command %q: examples are not normalized, make sure you are calling templates.Examples (from pkg/cmd/templates) before assigning cmd.Example`, cmdPath))
  93. }
  94. if trimmed := strings.TrimSpace(line); strings.HasPrefix(trimmed, "//") {
  95. errors = append(errors, fmt.Errorf(`command %q: we use # to start comments in examples instead of //`, cmdPath))
  96. }
  97. }
  98. }
  99. return errors
  100. }
  101. // CheckFlags checks if the command-line flags are valid
  102. func CheckFlags(cmd *cobra.Command) []error {
  103. allFlagsSlice := []*pflag.Flag{}
  104. cmd.Flags().VisitAll(func(f *pflag.Flag) {
  105. allFlagsSlice = append(allFlagsSlice, f)
  106. })
  107. cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
  108. allFlagsSlice = append(allFlagsSlice, f)
  109. })
  110. fmt.Fprintf(os.Stdout, " ↳ checking %d flags\n", len(allFlagsSlice))
  111. errors := []error{}
  112. // check flags long names
  113. regex, err := regexp.Compile(`^[a-z]+[a-z\-]*$`)
  114. if err != nil {
  115. errors = append(errors, fmt.Errorf("command %q: unable to compile regex to check flags", cmd.CommandPath()))
  116. return errors
  117. }
  118. for _, flag := range allFlagsSlice {
  119. name := flag.Name
  120. if !regex.MatchString(name) {
  121. errors = append(errors, fmt.Errorf("command %q: flag name %q is invalid, long form of flag names can only contain lowercase characters or dash (must match %v)", cmd.CommandPath(), name, regex))
  122. }
  123. }
  124. return errors
  125. }
  126. // CheckGlobalVarFlags checks if the global flags are valid
  127. func CheckGlobalVarFlags() []error {
  128. fmt.Fprint(os.Stdout, " ↳ checking flags from global vars\n")
  129. errors := []error{}
  130. pflag.CommandLine.VisitAll(func(f *pflag.Flag) {
  131. errors = append(errors, fmt.Errorf("flag %q is invalid, please don't register any flag under the global variable \"CommandLine\"", f.Name))
  132. })
  133. return errors
  134. }