compose.go 3.4 KB

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