client.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 client
  14. import (
  15. "context"
  16. "github.com/docker/compose-cli/api/compose"
  17. "github.com/docker/compose-cli/api/containers"
  18. "github.com/docker/compose-cli/api/secrets"
  19. "github.com/docker/compose-cli/backend"
  20. apicontext "github.com/docker/compose-cli/context"
  21. "github.com/docker/compose-cli/context/cloud"
  22. "github.com/docker/compose-cli/context/store"
  23. )
  24. // New returns a backend client associated with current context
  25. func New(ctx context.Context) (*Client, error) {
  26. currentContext := apicontext.CurrentContext(ctx)
  27. s := store.ContextStore(ctx)
  28. cc, err := s.Get(currentContext)
  29. if err != nil {
  30. return nil, err
  31. }
  32. service, err := backend.Get(ctx, cc.Type())
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &Client{
  37. backendType: cc.Type(),
  38. bs: service,
  39. }, nil
  40. }
  41. // GetCloudService returns a backend CloudService (typically login, create context)
  42. func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
  43. return backend.GetCloudService(ctx, backendType)
  44. }
  45. // Client is a multi-backend client
  46. type Client struct {
  47. backendType string
  48. bs backend.Service
  49. }
  50. // ContainerService returns the backend service for the current context
  51. func (c *Client) ContainerService() containers.Service {
  52. if cs := c.bs.ContainerService(); cs != nil {
  53. return cs
  54. }
  55. return &containerService{}
  56. }
  57. // ComposeService returns the backend service for the current context
  58. func (c *Client) ComposeService() compose.Service {
  59. if cs := c.bs.ComposeService(); cs != nil {
  60. return cs
  61. }
  62. return &composeService{}
  63. }
  64. // SecretsService returns the backend service for the current context
  65. func (c *Client) SecretsService() secrets.Service {
  66. if ss := c.bs.SecretsService(); ss != nil {
  67. return ss
  68. }
  69. return &secretsService{}
  70. }