compose.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. "context"
  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/client"
  21. "github.com/docker/compose-cli/context/store"
  22. "github.com/docker/compose-cli/errdefs"
  23. )
  24. type composeOptions struct {
  25. Name string
  26. DomainName string
  27. WorkingDir string
  28. ConfigPaths []string
  29. Environment []string
  30. Format string
  31. Detach bool
  32. Quiet bool
  33. }
  34. func addComposeCommonFlags(f *pflag.FlagSet, opts *composeOptions) {
  35. f.StringVarP(&opts.Name, "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.Name != "" {
  41. return o.Name, nil
  42. }
  43. options, err := o.toProjectOptions()
  44. if err != nil {
  45. return "", err
  46. }
  47. project, err := cli.ProjectFromOptions(options)
  48. if err != nil {
  49. return "", err
  50. }
  51. return project.Name, nil
  52. }
  53. func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  54. return cli.NewProjectOptions(o.ConfigPaths,
  55. cli.WithOsEnv,
  56. cli.WithEnv(o.Environment),
  57. cli.WithWorkingDirectory(o.WorkingDir),
  58. cli.WithName(o.Name))
  59. }
  60. // Command returns the compose command with its child commands
  61. func Command(contextType string) *cobra.Command {
  62. command := &cobra.Command{
  63. Short: "Docker Compose",
  64. Use: "compose",
  65. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  66. return checkComposeSupport(cmd.Context())
  67. },
  68. }
  69. command.AddCommand(
  70. upCommand(contextType),
  71. downCommand(),
  72. psCommand(),
  73. listCommand(),
  74. logsCommand(),
  75. convertCommand(),
  76. )
  77. if contextType == store.LocalContextType {
  78. command.AddCommand(
  79. buildCommand(),
  80. pushCommand(),
  81. pullCommand(),
  82. )
  83. }
  84. return command
  85. }
  86. func checkComposeSupport(ctx context.Context) error {
  87. _, err := client.New(ctx)
  88. if errdefs.IsNotFoundError(err) {
  89. return errdefs.ErrNotImplemented
  90. }
  91. return err
  92. }
  93. //
  94. func filter(project *types.Project, services []string) error {
  95. if len(services) == 0 {
  96. // All services
  97. return nil
  98. }
  99. names := map[string]bool{}
  100. err := addServiceNames(project, services, names)
  101. if err != nil {
  102. return err
  103. }
  104. var filtered types.Services
  105. for _, s := range project.Services {
  106. if _, ok := names[s.Name]; ok {
  107. filtered = append(filtered, s)
  108. }
  109. }
  110. project.Services = filtered
  111. return nil
  112. }
  113. func addServiceNames(project *types.Project, services []string, names map[string]bool) error {
  114. for _, name := range services {
  115. names[name] = true
  116. service, err := project.GetService(name)
  117. if err != nil {
  118. return err
  119. }
  120. err = addServiceNames(project, service.GetDependencies(), names)
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }