compose.go 3.1 KB

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