config.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // +build kube
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package utils
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "path/filepath"
  19. "regexp"
  20. "strings"
  21. "github.com/compose-spec/compose-go/loader"
  22. "github.com/compose-spec/compose-go/types"
  23. "github.com/prometheus/common/log"
  24. )
  25. var SupportedFilenames = []string{"compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml"}
  26. func GetConfigs(name string, configPaths []string) (string, []types.ConfigFile, error) {
  27. configPath, err := getConfigPaths(configPaths)
  28. if err != nil {
  29. return "", nil, err
  30. }
  31. if configPath == nil {
  32. return "", nil, nil
  33. }
  34. workingDir := filepath.Dir(configPath[0])
  35. if name == "" {
  36. name = os.Getenv("COMPOSE_PROJECT_NAME")
  37. }
  38. if name == "" {
  39. r := regexp.MustCompile(`[^a-z0-9\\-_]+`)
  40. name = r.ReplaceAllString(strings.ToLower(filepath.Base(workingDir)), "")
  41. }
  42. configs, err := parseConfigs(configPath)
  43. if err != nil {
  44. return "", nil, err
  45. }
  46. return workingDir, configs, nil
  47. }
  48. func getConfigPaths(configPaths []string) ([]string, error) {
  49. paths := []string{}
  50. pwd, err := os.Getwd()
  51. if err != nil {
  52. return nil, err
  53. }
  54. if len(configPaths) != 0 {
  55. for _, f := range configPaths {
  56. if f == "-" {
  57. paths = append(paths, f)
  58. continue
  59. }
  60. if !filepath.IsAbs(f) {
  61. f = filepath.Join(pwd, f)
  62. }
  63. if _, err := os.Stat(f); err != nil {
  64. return nil, err
  65. }
  66. paths = append(paths, f)
  67. }
  68. return paths, nil
  69. }
  70. sep := os.Getenv("COMPOSE_FILE_SEPARATOR")
  71. if sep == "" {
  72. sep = string(os.PathListSeparator)
  73. }
  74. f := os.Getenv("COMPOSE_FILE")
  75. if f != "" {
  76. return strings.Split(f, sep), nil
  77. }
  78. for {
  79. candidates := []string{}
  80. for _, n := range SupportedFilenames {
  81. f := filepath.Join(pwd, n)
  82. if _, err := os.Stat(f); err == nil {
  83. candidates = append(candidates, f)
  84. }
  85. }
  86. if len(candidates) > 0 {
  87. winner := candidates[0]
  88. if len(candidates) > 1 {
  89. log.Warnf("Found multiple config files with supported names: %s", strings.Join(candidates, ", "))
  90. log.Warnf("Using %s\n", winner)
  91. }
  92. return []string{winner}, nil
  93. }
  94. parent := filepath.Dir(pwd)
  95. if parent == pwd {
  96. return nil, nil
  97. }
  98. pwd = parent
  99. }
  100. }
  101. func parseConfigs(configPaths []string) ([]types.ConfigFile, error) {
  102. files := []types.ConfigFile{}
  103. for _, f := range configPaths {
  104. var (
  105. b []byte
  106. err error
  107. )
  108. if f == "-" {
  109. b, err = ioutil.ReadAll(os.Stdin)
  110. } else {
  111. if _, err := os.Stat(f); err != nil {
  112. return nil, err
  113. }
  114. b, err = ioutil.ReadFile(f)
  115. }
  116. if err != nil {
  117. return nil, err
  118. }
  119. config, err := loader.ParseYAML(b)
  120. if err != nil {
  121. return nil, err
  122. }
  123. files = append(files, types.ConfigFile{Filename: f, Config: config})
  124. }
  125. return files, nil
  126. }