client.go 3.1 KB

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