env_file.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2017 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 versioned
  14. import (
  15. "bufio"
  16. "bytes"
  17. "fmt"
  18. "os"
  19. "strings"
  20. "unicode"
  21. "unicode/utf8"
  22. "k8s.io/apimachinery/pkg/util/validation"
  23. )
  24. var utf8bom = []byte{0xEF, 0xBB, 0xBF}
  25. // proccessEnvFileLine returns a blank key if the line is empty or a comment.
  26. // The value will be retrieved from the environment if necessary.
  27. func proccessEnvFileLine(line []byte, filePath string,
  28. currentLine int) (key, value string, err error) {
  29. if !utf8.Valid(line) {
  30. return ``, ``, fmt.Errorf("env file %s contains invalid utf8 bytes at line %d: %v",
  31. filePath, currentLine+1, line)
  32. }
  33. // We trim UTF8 BOM from the first line of the file but no others
  34. if currentLine == 0 {
  35. line = bytes.TrimPrefix(line, utf8bom)
  36. }
  37. // trim the line from all leading whitespace first
  38. line = bytes.TrimLeftFunc(line, unicode.IsSpace)
  39. // If the line is empty or a comment, we return a blank key/value pair.
  40. if len(line) == 0 || line[0] == '#' {
  41. return ``, ``, nil
  42. }
  43. data := strings.SplitN(string(line), "=", 2)
  44. key = data[0]
  45. if errs := validation.IsEnvVarName(key); len(errs) != 0 {
  46. return ``, ``, fmt.Errorf("%q is not a valid key name: %s", key, strings.Join(errs, ";"))
  47. }
  48. if len(data) == 2 {
  49. value = data[1]
  50. } else {
  51. // No value (no `=` in the line) is a signal to obtain the value
  52. // from the environment.
  53. value = os.Getenv(key)
  54. }
  55. return
  56. }
  57. // addFromEnvFile processes an env file allows a generic addTo to handle the
  58. // collection of key value pairs or returns an error.
  59. func addFromEnvFile(filePath string, addTo func(key, value string) error) error {
  60. f, err := os.Open(filePath)
  61. if err != nil {
  62. return err
  63. }
  64. defer f.Close()
  65. scanner := bufio.NewScanner(f)
  66. currentLine := 0
  67. for scanner.Scan() {
  68. // Process the current line, retrieving a key/value pair if
  69. // possible.
  70. scannedBytes := scanner.Bytes()
  71. key, value, err := proccessEnvFileLine(scannedBytes, filePath, currentLine)
  72. if err != nil {
  73. return err
  74. }
  75. currentLine++
  76. if len(key) == 0 {
  77. // no key means line was empty or a comment
  78. continue
  79. }
  80. if err = addTo(key, value); err != nil {
  81. return err
  82. }
  83. }
  84. return nil
  85. }