compose.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2020 Docker, Inc.
  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/api/client"
  19. "github.com/docker/api/errdefs"
  20. )
  21. type composeOptions struct {
  22. Name string
  23. WorkingDir string
  24. ConfigPaths []string
  25. Environment []string
  26. }
  27. func (o *composeOptions) toProjectOptions() (*cli.ProjectOptions, error) {
  28. return cli.NewProjectOptions(o.ConfigPaths,
  29. cli.WithOsEnv,
  30. cli.WithEnv(o.Environment),
  31. cli.WithWorkingDirectory(o.WorkingDir),
  32. cli.WithName(o.Name))
  33. }
  34. // Command returns the compose command with its child commands
  35. func Command() *cobra.Command {
  36. command := &cobra.Command{
  37. Short: "Docker Compose",
  38. Use: "compose",
  39. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  40. return checkComposeSupport(cmd.Context())
  41. },
  42. }
  43. command.AddCommand(
  44. upCommand(),
  45. downCommand(),
  46. psCommand(),
  47. logsCommand(),
  48. convertCommand(),
  49. )
  50. return command
  51. }
  52. func checkComposeSupport(ctx context.Context) error {
  53. _, err := client.New(ctx)
  54. if errdefs.IsNotFoundError(err) {
  55. return errdefs.ErrNotImplemented
  56. }
  57. return err
  58. }