client.go 3.6 KB

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