compose.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. "context"
  16. "github.com/compose-spec/compose-go/cli"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/compose-cli/api/client"
  19. "github.com/docker/compose-cli/errdefs"
  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. }
  30. func (o *composeOptions) toProjectName() (string, error) {
  31. if o.Name != "" {
  32. return o.Name, nil
  33. }
  34. options, err := o.toProjectOptions()
  35. if err != nil {
  36. return "", err
  37. }
  38. project, err := cli.ProjectFromOptions(options)
  39. if err != nil {
  40. return "", err
  41. }
  42. return project.Name, nil
  43. }
  44. func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  45. return cli.NewProjectOptions(o.ConfigPaths,
  46. cli.WithOsEnv,
  47. cli.WithEnv(o.Environment),
  48. cli.WithWorkingDirectory(o.WorkingDir),
  49. cli.WithName(o.Name))
  50. }
  51. // Command returns the compose command with its child commands
  52. func Command(contextType string) *cobra.Command {
  53. command := &cobra.Command{
  54. Short: "Docker Compose",
  55. Use: "compose",
  56. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  57. return checkComposeSupport(cmd.Context())
  58. },
  59. }
  60. command.AddCommand(
  61. upCommand(contextType),
  62. downCommand(),
  63. psCommand(),
  64. listCommand(),
  65. logsCommand(),
  66. convertCommand(),
  67. )
  68. return command
  69. }
  70. func checkComposeSupport(ctx context.Context) error {
  71. _, err := client.New(ctx)
  72. if errdefs.IsNotFoundError(err) {
  73. return errdefs.ErrNotImplemented
  74. }
  75. return err
  76. }