compose.go 3.7 KB

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