client.go 3.1 KB

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