compose.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/api/context/store"
  20. )
  21. // Warning is a global warning to be displayed to user on command failure
  22. var Warning string
  23. type projectOptions struct {
  24. ProjectName string
  25. Profiles []string
  26. ConfigPaths []string
  27. WorkingDir string
  28. EnvFile string
  29. }
  30. func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
  31. f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
  32. f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
  33. f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  34. f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
  35. f.StringVar(&o.WorkingDir, "workdir", "", "Specify an alternate working directory")
  36. // TODO make --project-directory an alias
  37. }
  38. func (o *projectOptions) toProjectName() (string, error) {
  39. if o.ProjectName != "" {
  40. return o.ProjectName, nil
  41. }
  42. project, err := o.toProject(nil)
  43. if err != nil {
  44. return "", err
  45. }
  46. return project.Name, nil
  47. }
  48. func (o *projectOptions) toProject(services []string) (*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. if len(services) != 0 {
  58. s, err := project.GetServices(services...)
  59. if err != nil {
  60. return nil, err
  61. }
  62. o.Profiles = append(o.Profiles, s.GetProfiles()...)
  63. }
  64. project.ApplyProfiles(o.Profiles)
  65. err = project.ForServices(services)
  66. return project, err
  67. }
  68. func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  69. return cli.NewProjectOptions(o.ConfigPaths,
  70. cli.WithEnvFile(o.EnvFile),
  71. cli.WithDotEnv,
  72. cli.WithOsEnv,
  73. cli.WithWorkingDirectory(o.WorkingDir),
  74. cli.WithName(o.ProjectName))
  75. }
  76. // Command returns the compose command with its child commands
  77. func Command(contextType string) *cobra.Command {
  78. opts := projectOptions{}
  79. command := &cobra.Command{
  80. Short: "Docker Compose",
  81. Use: "compose",
  82. TraverseChildren: true,
  83. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  84. if contextType == store.DefaultContextType || contextType == store.LocalContextType {
  85. Warning = "The new 'docker compose' command is currently experimental. " +
  86. "To provide feedback or request new features please open issues at https://github.com/docker/compose-cli"
  87. }
  88. return nil
  89. },
  90. }
  91. command.AddCommand(
  92. upCommand(&opts, contextType),
  93. downCommand(&opts, contextType),
  94. startCommand(&opts),
  95. stopCommand(&opts),
  96. psCommand(&opts),
  97. listCommand(contextType),
  98. logsCommand(&opts, contextType),
  99. convertCommand(&opts),
  100. killCommand(&opts),
  101. runCommand(&opts),
  102. removeCommand(&opts),
  103. execCommand(&opts),
  104. pauseCommand(&opts),
  105. unpauseCommand(&opts),
  106. )
  107. if contextType == store.LocalContextType || contextType == store.DefaultContextType {
  108. command.AddCommand(
  109. buildCommand(&opts),
  110. pushCommand(&opts),
  111. pullCommand(&opts),
  112. createCommand(&opts),
  113. )
  114. }
  115. command.Flags().SetInterspersed(false)
  116. opts.addProjectFlags(command.Flags())
  117. return command
  118. }