mirror of https://github.com/docker/compose.git
Refactor example/backend
Also promote init function from returning interface{} to backend.Service to avoid typecasting Signed-off-by: Ulysses Souza <ulyssessouza@gmail.com>
This commit is contained in:
parent
6a2ce681b4
commit
73529cc264
|
@ -22,7 +22,7 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
backend.Register("aci", "aci", func(ctx context.Context) (interface{}, error) {
|
||||
backend.Register("aci", "aci", func(ctx context.Context) (backend.Service, error) {
|
||||
return New(ctx)
|
||||
})
|
||||
}
|
||||
|
@ -53,11 +53,11 @@ func New(ctx context.Context) (backend.Service, error) {
|
|||
|
||||
func getAciAPIService(cgc containerinstance.ContainerGroupsClient, aciCtx store.AciContext) *aciAPIService {
|
||||
return &aciAPIService{
|
||||
container: aciContainerService{
|
||||
aciContainerService: aciContainerService{
|
||||
containerGroupsClient: cgc,
|
||||
ctx: aciCtx,
|
||||
},
|
||||
compose: aciComposeService{
|
||||
aciComposeService: aciComposeService{
|
||||
containerGroupsClient: cgc,
|
||||
ctx: aciCtx,
|
||||
},
|
||||
|
@ -65,21 +65,21 @@ func getAciAPIService(cgc containerinstance.ContainerGroupsClient, aciCtx store.
|
|||
}
|
||||
|
||||
type aciAPIService struct {
|
||||
container aciContainerService
|
||||
compose aciComposeService
|
||||
aciContainerService
|
||||
aciComposeService
|
||||
}
|
||||
|
||||
func (a *aciAPIService) ContainerService() containers.Service {
|
||||
return &aciContainerService{
|
||||
containerGroupsClient: a.container.containerGroupsClient,
|
||||
ctx: a.container.ctx,
|
||||
containerGroupsClient: a.aciContainerService.containerGroupsClient,
|
||||
ctx: a.aciContainerService.ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *aciAPIService) ComposeService() compose.Service {
|
||||
return &aciComposeService{
|
||||
containerGroupsClient: a.compose.containerGroupsClient,
|
||||
ctx: a.compose.ctx,
|
||||
containerGroupsClient: a.aciComposeService.containerGroupsClient,
|
||||
ctx: a.aciComposeService.ctx,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ var (
|
|||
errTypeRegistered = errors.New("backend: already registered")
|
||||
)
|
||||
|
||||
type initFunc func(context.Context) (interface{}, error)
|
||||
type initFunc func(context.Context) (Service, error)
|
||||
|
||||
type registeredBackend struct {
|
||||
name string
|
||||
|
@ -58,7 +58,7 @@ func Register(name string, backendType string, init initFunc) {
|
|||
|
||||
// Get returns the backend registered for a particular type, it returns
|
||||
// an error if there is no registered backends for the given type.
|
||||
func Get(ctx context.Context, backendType string) (interface{}, error) {
|
||||
func Get(ctx context.Context, backendType string) (Service, error) {
|
||||
for _, b := range backends.r {
|
||||
if b.backendType == backendType {
|
||||
return b.init(ctx)
|
||||
|
|
|
@ -29,7 +29,6 @@ package client
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/docker/api/backend"
|
||||
backendv1 "github.com/docker/api/backend/v1"
|
||||
|
@ -53,15 +52,11 @@ func New(ctx context.Context) (*Client, error) {
|
|||
}
|
||||
contextType := s.GetType(cc)
|
||||
|
||||
b, err := backend.Get(ctx, contextType)
|
||||
service, err := backend.Get(ctx, contextType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
service, ok := b.(backend.Service)
|
||||
if !ok {
|
||||
return nil, errors.New("backend not found")
|
||||
}
|
||||
return &Client{
|
||||
backendType: contextType,
|
||||
bs: service,
|
||||
|
|
|
@ -6,17 +6,31 @@ import (
|
|||
"io"
|
||||
|
||||
"github.com/docker/api/backend"
|
||||
"github.com/docker/api/compose"
|
||||
"github.com/docker/api/containers"
|
||||
)
|
||||
|
||||
type containerService struct{}
|
||||
type apiService struct {
|
||||
containerService
|
||||
composeService
|
||||
}
|
||||
|
||||
func (a *apiService) ContainerService() containers.Service {
|
||||
return &a.containerService
|
||||
}
|
||||
|
||||
func (a *apiService) ComposeService() compose.Service {
|
||||
return &a.composeService
|
||||
}
|
||||
|
||||
func init() {
|
||||
backend.Register("example", "example", func(ctx context.Context) (interface{}, error) {
|
||||
return &containerService{}, nil
|
||||
backend.Register("example", "example", func(ctx context.Context) (backend.Service, error) {
|
||||
return &apiService{}, nil
|
||||
})
|
||||
}
|
||||
|
||||
type containerService struct{}
|
||||
|
||||
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
|
||||
return []containers.Container{
|
||||
{
|
||||
|
@ -44,3 +58,23 @@ func (cs *containerService) Logs(ctx context.Context, containerName string, requ
|
|||
fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
|
||||
return nil
|
||||
}
|
||||
|
||||
type composeService struct{}
|
||||
|
||||
func (cs *composeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
|
||||
prj, err := compose.ProjectFromOptions(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Up command on project %q", prj.Name)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *composeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
|
||||
prj, err := compose.ProjectFromOptions(&opts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("Down command on project %q", prj.Name)
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue