compose.go 3.8 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 composeOptions struct {
  23. ProjectName string
  24. DomainName string
  25. WorkingDir string
  26. ConfigPaths []string
  27. Environment []string
  28. EnvFile string
  29. Format string
  30. Detach bool
  31. Build bool
  32. Quiet bool
  33. }
  34. func addComposeCommonFlags(f *pflag.FlagSet, opts *composeOptions) {
  35. f.StringVarP(&opts.ProjectName, "project-name", "p", "", "Project name")
  36. f.StringVar(&opts.Format, "format", "", "Format the output. Values: [pretty | json]. (Default: pretty)")
  37. f.BoolVarP(&opts.Quiet, "quiet", "q", false, "Only display IDs")
  38. }
  39. func (o *composeOptions) toProjectName() (string, error) {
  40. if o.ProjectName != "" {
  41. return o.ProjectName, nil
  42. }
  43. project, err := o.toProject()
  44. if err != nil {
  45. return "", err
  46. }
  47. return project.Name, nil
  48. }
  49. func (o *composeOptions) toProject() (*types.Project, error) {
  50. options, err := o.toProjectOptions()
  51. if err != nil {
  52. return nil, err
  53. }
  54. project, err := cli.ProjectFromOptions(options)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return project, nil
  59. }
  60. func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  61. return cli.NewProjectOptions(o.ConfigPaths,
  62. cli.WithOsEnv,
  63. cli.WithEnvFile(o.EnvFile),
  64. cli.WithDotEnv,
  65. cli.WithEnv(o.Environment),
  66. cli.WithWorkingDirectory(o.WorkingDir),
  67. cli.WithName(o.ProjectName))
  68. }
  69. // Command returns the compose command with its child commands
  70. func Command(contextType string) *cobra.Command {
  71. command := &cobra.Command{
  72. Short: "Docker Compose",
  73. Use: "compose",
  74. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  75. if contextType == store.DefaultContextType || contextType == store.LocalContextType {
  76. 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")
  77. }
  78. return nil
  79. },
  80. }
  81. command.AddCommand(
  82. upCommand(contextType),
  83. downCommand(),
  84. psCommand(),
  85. listCommand(),
  86. logsCommand(),
  87. convertCommand(),
  88. runCommand(),
  89. )
  90. if contextType == store.LocalContextType || contextType == store.DefaultContextType {
  91. command.AddCommand(
  92. buildCommand(),
  93. pushCommand(),
  94. pullCommand(),
  95. )
  96. }
  97. command.Flags().SetInterspersed(false)
  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. }