compose.go 3.2 KB

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