compose.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/pkg/errors"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/api/client"
  19. apicontext "github.com/docker/api/context"
  20. "github.com/docker/api/context/store"
  21. "github.com/docker/api/errdefs"
  22. )
  23. // Command returns the compose command with its child commands
  24. func Command() *cobra.Command {
  25. command := &cobra.Command{
  26. Short: "Docker Compose",
  27. Use: "compose",
  28. PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
  29. return checkComposeSupport(cmd.Context())
  30. },
  31. }
  32. command.AddCommand(
  33. upCommand(),
  34. downCommand(),
  35. psCommand(),
  36. logsCommand(),
  37. )
  38. return command
  39. }
  40. func checkComposeSupport(ctx context.Context) error {
  41. c, err := client.New(ctx)
  42. if err == nil {
  43. composeService := c.ComposeService()
  44. if composeService == nil {
  45. return errors.New("compose not implemented in current context")
  46. }
  47. return nil
  48. }
  49. currentContext := apicontext.CurrentContext(ctx)
  50. s := store.ContextStore(ctx)
  51. cc, err := s.Get(currentContext)
  52. if err != nil {
  53. return err
  54. }
  55. switch cc.Type() {
  56. case store.AwsContextType:
  57. return errors.New("use 'docker ecs compose' on context type " + cc.Type())
  58. default:
  59. return errors.Wrapf(errdefs.ErrNotImplemented, "compose command not supported on context type %q", cc.Type())
  60. }
  61. }