kubectl_dash_f.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2015 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. "fmt"
  16. "os"
  17. "path"
  18. "strings"
  19. )
  20. // Looks for lines that have kubectl commands with -f flags and files that
  21. // don't exist.
  22. func updateKubectlFileTargets(file string, mlines mungeLines) (mungeLines, error) {
  23. var errors []string
  24. for i, mline := range mlines {
  25. if !mline.preformatted {
  26. continue
  27. }
  28. if err := lookForKubectl(mline.data, i); err != nil {
  29. errors = append(errors, err.Error())
  30. }
  31. }
  32. err := error(nil)
  33. if len(errors) != 0 {
  34. err = fmt.Errorf("%s", strings.Join(errors, "\n"))
  35. }
  36. return mlines, err
  37. }
  38. func lookForKubectl(line string, lineNum int) error {
  39. fields := strings.Fields(line)
  40. for i := range fields {
  41. if fields[i] == "kubectl" {
  42. return gotKubectl(lineNum, fields, i)
  43. }
  44. }
  45. return nil
  46. }
  47. func gotKubectl(lineNum int, fields []string, fieldNum int) error {
  48. for i := fieldNum + 1; i < len(fields); i++ {
  49. switch fields[i] {
  50. case "create", "update", "replace", "delete":
  51. return gotCommand(lineNum, fields, i)
  52. }
  53. }
  54. return nil
  55. }
  56. func gotCommand(lineNum int, fields []string, fieldNum int) error {
  57. for i := fieldNum + 1; i < len(fields); i++ {
  58. if strings.HasPrefix(fields[i], "-f") {
  59. return gotDashF(lineNum, fields, i)
  60. }
  61. }
  62. return nil
  63. }
  64. func gotDashF(lineNum int, fields []string, fieldNum int) error {
  65. target := ""
  66. if fields[fieldNum] == "-f" {
  67. if fieldNum+1 == len(fields) {
  68. return fmt.Errorf("ran out of fields after '-f'")
  69. }
  70. target = fields[fieldNum+1]
  71. } else {
  72. target = fields[fieldNum][2:]
  73. }
  74. // Turn dirs into file-like names.
  75. target = strings.TrimRight(target, "/")
  76. // Now exclude special-cases
  77. if target == "-" || target == "FILENAME" {
  78. // stdin and "FILENAME" are OK
  79. return nil
  80. }
  81. if strings.HasPrefix(target, "http://") || strings.HasPrefix(target, "https://") {
  82. // URLs are ok
  83. return nil
  84. }
  85. if strings.HasPrefix(target, "./") {
  86. // Same-dir files are usually created in the same example
  87. return nil
  88. }
  89. if strings.HasPrefix(target, "~/") {
  90. // Home directory may also be created by the same example
  91. return nil
  92. }
  93. if strings.HasPrefix(target, "/") {
  94. // Absolute paths tend to be /tmp/* and created in the same example.
  95. return nil
  96. }
  97. if strings.HasPrefix(target, "$") {
  98. // Allow the start of the target to be an environment
  99. // variable that points to the root of the kubernetes
  100. // repo.
  101. split := strings.SplitN(target, "/", 2)
  102. if len(split) == 2 {
  103. target = split[1]
  104. }
  105. }
  106. // If we got here we expect the file to exist.
  107. _, err := os.Stat(path.Join(repoRoot, target))
  108. if os.IsNotExist(err) {
  109. return fmt.Errorf("%d: target file %q does not exist", lineNum, target)
  110. }
  111. return err
  112. }