compose.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/morikuni/aec"
  19. "github.com/pkg/errors"
  20. "github.com/spf13/cobra"
  21. "github.com/spf13/pflag"
  22. "github.com/docker/compose-cli/api/context/store"
  23. )
  24. // Warning is a global warning to be displayed to user on command failure
  25. var Warning string
  26. type projectOptions struct {
  27. ProjectName string
  28. Profiles []string
  29. ConfigPaths []string
  30. WorkDir string
  31. ProjectDir string
  32. EnvFile string
  33. }
  34. func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
  35. f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
  36. f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
  37. f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  38. f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
  39. f.StringVar(&o.ProjectDir, "project-directory", "", "Specify an alternate working directory\n(default: the path of the Compose file)")
  40. f.StringVar(&o.WorkDir, "workdir", "", "DEPRECATED! USE --project-directory INSTEAD.\nSpecify an alternate working directory\n(default: the path of the Compose file)")
  41. _ = f.MarkHidden("workdir")
  42. }
  43. func (o *projectOptions) toProjectName() (string, error) {
  44. if o.ProjectName != "" {
  45. return o.ProjectName, nil
  46. }
  47. project, err := o.toProject(nil)
  48. if err != nil {
  49. return "", err
  50. }
  51. return project.Name, nil
  52. }
  53. func (o *projectOptions) toProject(services []string) (*types.Project, error) {
  54. options, err := o.toProjectOptions()
  55. if err != nil {
  56. return nil, err
  57. }
  58. project, err := cli.ProjectFromOptions(options)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if len(services) != 0 {
  63. s, err := project.GetServices(services...)
  64. if err != nil {
  65. return nil, err
  66. }
  67. o.Profiles = append(o.Profiles, s.GetProfiles()...)
  68. }
  69. project.ApplyProfiles(o.Profiles)
  70. err = project.ForServices(services)
  71. return project, err
  72. }
  73. func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  74. return cli.NewProjectOptions(o.ConfigPaths,
  75. cli.WithEnvFile(o.EnvFile),
  76. cli.WithDotEnv,
  77. cli.WithOsEnv,
  78. cli.WithWorkingDirectory(o.ProjectDir),
  79. cli.WithName(o.ProjectName))
  80. }
  81. // Command returns the compose command with its child commands
  82. func Command(contextType string) *cobra.Command {
  83. opts := projectOptions{}
  84. command := &cobra.Command{
  85. Short: "Docker Compose",
  86. Use: "compose",
  87. TraverseChildren: true,
  88. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  89. if opts.WorkDir != "" {
  90. if opts.ProjectDir != "" {
  91. return errors.New(aec.Apply(`cannot specify DEPRECATED "--workdir" and "--project-directory". Please use only "--project-directory" instead.`, aec.RedF))
  92. }
  93. opts.ProjectDir = opts.WorkDir
  94. fmt.Println(aec.Apply(`option "--workdir" is DEPRECATED at root level! Please use "--project-directory" instead.`, aec.RedF))
  95. }
  96. if contextType == store.DefaultContextType || contextType == store.LocalContextType {
  97. Warning = "The new 'docker compose' command is currently experimental. " +
  98. "To provide feedback or request new features please open issues at https://github.com/docker/compose-cli"
  99. }
  100. return nil
  101. },
  102. }
  103. command.AddCommand(
  104. upCommand(&opts, contextType),
  105. downCommand(&opts, contextType),
  106. startCommand(&opts),
  107. stopCommand(&opts),
  108. psCommand(&opts),
  109. listCommand(contextType),
  110. logsCommand(&opts, contextType),
  111. convertCommand(&opts),
  112. killCommand(&opts),
  113. runCommand(&opts),
  114. removeCommand(&opts),
  115. execCommand(&opts),
  116. pauseCommand(&opts),
  117. unpauseCommand(&opts),
  118. topCommand(&opts),
  119. )
  120. if contextType == store.LocalContextType || contextType == store.DefaultContextType {
  121. command.AddCommand(
  122. buildCommand(&opts),
  123. pushCommand(&opts),
  124. pullCommand(&opts),
  125. createCommand(&opts),
  126. )
  127. }
  128. command.Flags().SetInterspersed(false)
  129. opts.addProjectFlags(command.Flags())
  130. return command
  131. }