compose.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 projectOptions struct {
  23. ProjectName string
  24. Profiles []string
  25. ConfigPaths []string
  26. WorkingDir string
  27. EnvFile string
  28. }
  29. func (o *projectOptions) addProjectFlags(f *pflag.FlagSet) {
  30. f.StringArrayVar(&o.Profiles, "profile", []string{}, "Specify a profile to enable")
  31. f.StringVarP(&o.ProjectName, "project-name", "p", "", "Project name")
  32. f.StringArrayVarP(&o.ConfigPaths, "file", "f", []string{}, "Compose configuration files")
  33. f.StringVar(&o.EnvFile, "env-file", "", "Specify an alternate environment file.")
  34. f.StringVar(&o.WorkingDir, "workdir", "", "Specify an alternate working directory")
  35. // TODO make --project-directory an alias
  36. }
  37. func (o *projectOptions) toProjectName() (string, error) {
  38. if o.ProjectName != "" {
  39. return o.ProjectName, nil
  40. }
  41. project, err := o.toProject(nil)
  42. if err != nil {
  43. return "", err
  44. }
  45. return project.Name, nil
  46. }
  47. func (o *projectOptions) toProject(services []string) (*types.Project, error) {
  48. options, err := o.toProjectOptions()
  49. if err != nil {
  50. return nil, err
  51. }
  52. project, err := cli.ProjectFromOptions(options)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if len(services) != 0 {
  57. s, err := project.GetServices(services)
  58. if err != nil {
  59. return nil, err
  60. }
  61. o.Profiles = append(o.Profiles, s.GetProfiles()...)
  62. }
  63. project.ApplyProfiles(o.Profiles)
  64. err = project.ForServices(services)
  65. return project, err
  66. }
  67. func (o *projectOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  68. return cli.NewProjectOptions(o.ConfigPaths,
  69. cli.WithOsEnv,
  70. cli.WithEnvFile(o.EnvFile),
  71. cli.WithDotEnv,
  72. cli.WithWorkingDirectory(o.WorkingDir),
  73. cli.WithName(o.ProjectName))
  74. }
  75. // Command returns the compose command with its child commands
  76. func Command(contextType string) *cobra.Command {
  77. opts := projectOptions{}
  78. command := &cobra.Command{
  79. Short: "Docker Compose",
  80. Use: "compose",
  81. TraverseChildren: true,
  82. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  83. if contextType == store.DefaultContextType || contextType == store.LocalContextType {
  84. 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")
  85. }
  86. return nil
  87. },
  88. }
  89. command.AddCommand(
  90. upCommand(&opts, contextType),
  91. downCommand(&opts),
  92. startCommand(&opts),
  93. stopCommand(&opts),
  94. psCommand(&opts),
  95. listCommand(),
  96. logsCommand(&opts, contextType),
  97. convertCommand(&opts),
  98. killCommand(&opts),
  99. runCommand(&opts),
  100. removeCommand(&opts),
  101. )
  102. if contextType == store.LocalContextType || contextType == store.DefaultContextType {
  103. command.AddCommand(
  104. buildCommand(&opts),
  105. pushCommand(&opts),
  106. pullCommand(&opts),
  107. createCommand(&opts),
  108. )
  109. }
  110. command.Flags().SetInterspersed(false)
  111. opts.addProjectFlags(command.Flags())
  112. return command
  113. }