compose.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. psCommand(&opts),
  81. listCommand(),
  82. logsCommand(&opts),
  83. convertCommand(&opts),
  84. runCommand(&opts),
  85. )
  86. if contextType == store.LocalContextType || contextType == store.DefaultContextType {
  87. command.AddCommand(
  88. buildCommand(&opts),
  89. pushCommand(&opts),
  90. pullCommand(&opts),
  91. )
  92. }
  93. command.Flags().SetInterspersed(false)
  94. opts.addProjectFlags(command.PersistentFlags())
  95. return command
  96. }
  97. //
  98. func filter(project *types.Project, services []string) error {
  99. if len(services) == 0 {
  100. // All services
  101. return nil
  102. }
  103. names := map[string]bool{}
  104. err := addServiceNames(project, services, names)
  105. if err != nil {
  106. return err
  107. }
  108. var filtered types.Services
  109. for _, s := range project.Services {
  110. if _, ok := names[s.Name]; ok {
  111. filtered = append(filtered, s)
  112. }
  113. }
  114. project.Services = filtered
  115. return nil
  116. }
  117. func addServiceNames(project *types.Project, services []string, names map[string]bool) error {
  118. for _, name := range services {
  119. names[name] = true
  120. service, err := project.GetService(name)
  121. if err != nil {
  122. return err
  123. }
  124. err = addServiceNames(project, service.GetDependencies(), names)
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. return nil
  130. }