compose.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. Copyright 2020 Docker Compose CLI 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 compose
  14. import (
  15. "fmt"
  16. "github.com/compose-spec/compose-go/cli"
  17. "github.com/compose-spec/compose-go/types"
  18. "github.com/spf13/cobra"
  19. "github.com/spf13/pflag"
  20. "github.com/docker/compose-cli/api/context/store"
  21. )
  22. type projectOptions struct {
  23. ProjectName string
  24. ConfigPaths []string
  25. WorkingDir string
  26. EnvFile string
  27. }
  28. func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
  29. f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
  30. f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  31. f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
  32. f.StringVar(&o.WorkingDir, "workdir", "", "Specify an alternate working directory")
  33. // TODO make --project-directory an alias
  34. }
  35. func (o *projectOptions) toProjectName() (string, error) {
  36. if o.ProjectName != "" {
  37. return o.ProjectName, nil
  38. }
  39. project, err := o.toProject()
  40. if err != nil {
  41. return "", err
  42. }
  43. return project.Name, nil
  44. }
  45. func (o *projectOptions) toProject() (*types.Project, error) {
  46. options, err := o.toProjectOptions()
  47. if err != nil {
  48. return nil, err
  49. }
  50. project, err := cli.ProjectFromOptions(options)
  51. if err != nil {
  52. return nil, err
  53. }
  54. return project, nil
  55. }
  56. func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  57. return cli.NewProjectOptions(o.ConfigPaths,
  58. cli.WithOsEnv,
  59. cli.WithEnvFile(o.EnvFile),
  60. cli.WithDotEnv,
  61. cli.WithWorkingDirectory(o.WorkingDir),
  62. cli.WithName(o.ProjectName))
  63. }
  64. // Command returns the compose command with its child commands
  65. func Command(contextType string) *cobra.Command {
  66. opts := projectOptions{}
  67. command := &cobra.Command{
  68. Short: "Docker Compose",
  69. Use: "compose",
  70. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  71. if contextType == store.DefaultContextType || contextType == store.LocalContextType {
  72. fmt.Println("The new 'docker compose' command is currently experimental. To provide feedback or request new features please open issues at https://github.com/docker/compose-cli")
  73. }
  74. return nil
  75. },
  76. }
  77. command.AddCommand(
  78. upCommand(&opts, contextType),
  79. downCommand(&opts),
  80. startCommand(&opts),
  81. stopCommand(&opts),
  82. psCommand(&opts),
  83. listCommand(),
  84. logsCommand(&opts, contextType),
  85. convertCommand(&opts),
  86. runCommand(&opts),
  87. )
  88. if contextType == store.LocalContextType || contextType == store.DefaultContextType {
  89. command.AddCommand(
  90. buildCommand(&opts),
  91. pushCommand(&opts),
  92. pullCommand(&opts),
  93. createCommand(&opts),
  94. )
  95. }
  96. command.Flags().SetInterspersed(false)
  97. opts.addProjectFlags(command.PersistentFlags())
  98. return command
  99. }
  100. //
  101. func filter(project *types.Project, services []string) error {
  102. if len(services) == 0 {
  103. // All services
  104. return nil
  105. }
  106. names := map[string]bool{}
  107. err := addServiceNames(project, services, names)
  108. if err != nil {
  109. return err
  110. }
  111. var filtered types.Services
  112. for _, s := range project.Services {
  113. if _, ok := names[s.Name]; ok {
  114. filtered = append(filtered, s)
  115. }
  116. }
  117. project.Services = filtered
  118. return nil
  119. }
  120. func addServiceNames(project *types.Project, services []string, names map[string]bool) error {
  121. for _, name := range services {
  122. names[name] = true
  123. service, err := project.GetService(name)
  124. if err != nil {
  125. return err
  126. }
  127. err = addServiceNames(project, service.GetDependencies(), names)
  128. if err != nil {
  129. return err
  130. }
  131. }
  132. return nil
  133. }