client.go 2.6 KB

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