client.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/secrets"
  19. "github.com/docker/compose-cli/api/volumes"
  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. client := NewClient(cc.Type(), service)
  38. return &client, nil
  39. }
  40. // NewClient returns new client
  41. func NewClient(backendType string, service backend.Service) Client {
  42. return Client{
  43. backendType: backendType,
  44. bs: service,
  45. }
  46. }
  47. // GetCloudService returns a backend CloudService (typically login, create context)
  48. func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
  49. return backend.GetCloudService(ctx, backendType)
  50. }
  51. // Client is a multi-backend client
  52. type Client struct {
  53. backendType string
  54. bs backend.Service
  55. }
  56. // ContextType the context type associated with backend
  57. func (c *Client) ContextType() string {
  58. return c.backendType
  59. }
  60. // ContainerService returns the backend service for the current context
  61. func (c *Client) ContainerService() containers.Service {
  62. if cs := c.bs.ContainerService(); cs != nil {
  63. return cs
  64. }
  65. return &containerService{}
  66. }
  67. // ComposeService returns the backend service for the current context
  68. func (c *Client) ComposeService() compose.Service {
  69. if cs := c.bs.ComposeService(); cs != nil {
  70. return cs
  71. }
  72. return &composeService{}
  73. }
  74. // SecretsService returns the backend service for the current context
  75. func (c *Client) SecretsService() secrets.Service {
  76. if ss := c.bs.SecretsService(); ss != nil {
  77. return ss
  78. }
  79. return &secretsService{}
  80. }
  81. // VolumeService returns the backend service for the current context
  82. func (c *Client) VolumeService() volumes.Service {
  83. if vs := c.bs.VolumeService(); vs != nil {
  84. return vs
  85. }
  86. return &volumeService{}
  87. }