compose.go 4.8 KB

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