2020-09-07 13:22:08 +02:00
|
|
|
/*
|
2020-09-22 12:13:00 +02:00
|
|
|
Copyright 2020 Docker Compose CLI authors
|
2020-09-07 13:22:08 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-07 18:23:28 +02:00
|
|
|
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/api/compose"
|
|
|
|
"github.com/docker/compose-cli/api/containers"
|
|
|
|
"github.com/docker/compose-cli/api/secrets"
|
2020-09-10 14:17:49 +02:00
|
|
|
"github.com/docker/compose-cli/api/volumes"
|
2020-09-07 13:22:08 +02:00
|
|
|
"github.com/docker/compose-cli/backend"
|
|
|
|
apicontext "github.com/docker/compose-cli/context"
|
|
|
|
"github.com/docker/compose-cli/context/cloud"
|
|
|
|
"github.com/docker/compose-cli/context/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
// New returns a backend client associated with current context
|
|
|
|
func New(ctx context.Context) (*Client, error) {
|
|
|
|
currentContext := apicontext.CurrentContext(ctx)
|
|
|
|
s := store.ContextStore(ctx)
|
|
|
|
|
|
|
|
cc, err := s.Get(currentContext)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
service, err := backend.Get(ctx, cc.Type())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:26:48 +02:00
|
|
|
client := NewClient(cc.Type(), service)
|
|
|
|
return &client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient returns new client
|
|
|
|
func NewClient(backendType string, service backend.Service) Client {
|
|
|
|
return Client{
|
|
|
|
backendType: backendType,
|
2020-09-07 13:22:08 +02:00
|
|
|
bs: service,
|
2020-09-25 16:26:48 +02:00
|
|
|
}
|
2020-09-07 13:22:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCloudService returns a backend CloudService (typically login, create context)
|
|
|
|
func GetCloudService(ctx context.Context, backendType string) (cloud.Service, error) {
|
|
|
|
return backend.GetCloudService(ctx, backendType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Client is a multi-backend client
|
|
|
|
type Client struct {
|
|
|
|
backendType string
|
|
|
|
bs backend.Service
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:26:48 +02:00
|
|
|
// ContextType the context type associated with backend
|
|
|
|
func (c *Client) ContextType() string {
|
|
|
|
return c.backendType
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:22:08 +02:00
|
|
|
// ContainerService returns the backend service for the current context
|
|
|
|
func (c *Client) ContainerService() containers.Service {
|
|
|
|
if cs := c.bs.ContainerService(); cs != nil {
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
|
|
|
return &containerService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ComposeService returns the backend service for the current context
|
|
|
|
func (c *Client) ComposeService() compose.Service {
|
|
|
|
if cs := c.bs.ComposeService(); cs != nil {
|
|
|
|
return cs
|
|
|
|
}
|
|
|
|
|
|
|
|
return &composeService{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SecretsService returns the backend service for the current context
|
|
|
|
func (c *Client) SecretsService() secrets.Service {
|
|
|
|
if ss := c.bs.SecretsService(); ss != nil {
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
return &secretsService{}
|
|
|
|
}
|
2020-09-07 18:23:28 +02:00
|
|
|
|
2020-09-07 14:39:19 +02:00
|
|
|
// VolumeService returns the backend service for the current context
|
|
|
|
func (c *Client) VolumeService() volumes.Service {
|
|
|
|
if vs := c.bs.VolumeService(); vs != nil {
|
|
|
|
return vs
|
|
|
|
}
|
|
|
|
|
|
|
|
return &volumeService{}
|
|
|
|
}
|