project.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package compose
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. "github.com/compose-spec/compose-go/loader"
  10. "github.com/compose-spec/compose-go/types"
  11. "github.com/sirupsen/logrus"
  12. )
  13. var supportedFilenames = []string{
  14. "compose.yml",
  15. "compose.yaml",
  16. "docker-compose.yml",
  17. "docker-compose.yaml",
  18. }
  19. // ProjectOptions configures a compose project
  20. type ProjectOptions struct {
  21. Name string
  22. WorkDir string
  23. ConfigPaths []string
  24. Environment []string
  25. }
  26. // Project represents a compose project with a name
  27. type Project struct {
  28. types.Config
  29. projectDir string
  30. Name string `yaml:"-" json:"-"`
  31. }
  32. // ProjectFromOptions load a compose project based on given options
  33. func ProjectFromOptions(options *ProjectOptions) (*Project, error) {
  34. configPath, err := getConfigPathFromOptions(options)
  35. if err != nil {
  36. return nil, err
  37. }
  38. configs, err := parseConfigs(configPath)
  39. if err != nil {
  40. return nil, err
  41. }
  42. name := options.Name
  43. if name == "" {
  44. r := regexp.MustCompile(`[^a-z0-9\\-_]+`)
  45. absPath, err := filepath.Abs(options.WorkDir)
  46. if err != nil {
  47. return nil, err
  48. }
  49. name = r.ReplaceAllString(strings.ToLower(filepath.Base(absPath)), "")
  50. }
  51. return newProject(types.ConfigDetails{
  52. WorkingDir: options.WorkDir,
  53. ConfigFiles: configs,
  54. Environment: getAsEqualsMap(options.Environment),
  55. }, name)
  56. }
  57. func newProject(config types.ConfigDetails, name string) (*Project, error) {
  58. model, err := loader.Load(config)
  59. if err != nil {
  60. return nil, err
  61. }
  62. p := Project{
  63. Config: *model,
  64. projectDir: config.WorkingDir,
  65. Name: name,
  66. }
  67. return &p, nil
  68. }
  69. func getConfigPathFromOptions(options *ProjectOptions) ([]string, error) {
  70. var paths []string
  71. pwd := options.WorkDir
  72. if len(options.ConfigPaths) != 0 {
  73. for _, f := range options.ConfigPaths {
  74. if f == "-" {
  75. paths = append(paths, f)
  76. continue
  77. }
  78. if !filepath.IsAbs(f) {
  79. f = filepath.Join(pwd, f)
  80. }
  81. if _, err := os.Stat(f); err != nil {
  82. return nil, err
  83. }
  84. paths = append(paths, f)
  85. }
  86. return paths, nil
  87. }
  88. for {
  89. var candidates []string
  90. for _, n := range supportedFilenames {
  91. f := filepath.Join(pwd, n)
  92. if _, err := os.Stat(f); err == nil {
  93. candidates = append(candidates, f)
  94. }
  95. }
  96. if len(candidates) > 0 {
  97. winner := candidates[0]
  98. if len(candidates) > 1 {
  99. logrus.Warnf("Found multiple config files with supported names: %s", strings.Join(candidates, ", "))
  100. logrus.Warnf("Using %s\n", winner)
  101. }
  102. return []string{winner}, nil
  103. }
  104. parent := filepath.Dir(pwd)
  105. if parent == pwd {
  106. return nil, fmt.Errorf("can't find a suitable configuration file in this directory or any parent. Is %q the right directory?", pwd)
  107. }
  108. pwd = parent
  109. }
  110. }
  111. func parseConfigs(configPaths []string) ([]types.ConfigFile, error) {
  112. var files []types.ConfigFile
  113. for _, f := range configPaths {
  114. var b []byte
  115. var err error
  116. if f == "-" {
  117. b, err = ioutil.ReadAll(os.Stdin)
  118. } else {
  119. b, err = ioutil.ReadFile(f)
  120. }
  121. if err != nil {
  122. return nil, err
  123. }
  124. config, err := loader.ParseYAML(b)
  125. if err != nil {
  126. return nil, err
  127. }
  128. files = append(files, types.ConfigFile{Filename: f, Config: config})
  129. }
  130. return files, nil
  131. }
  132. // getAsEqualsMap split key=value formatted strings into a key : value map
  133. func getAsEqualsMap(em []string) map[string]string {
  134. m := make(map[string]string)
  135. for _, v := range em {
  136. kv := strings.SplitN(v, "=", 2)
  137. m[kv[0]] = kv[1]
  138. }
  139. return m
  140. }