diff --git a/azure/backend.go b/azure/backend.go index c6651f9e8..639110d15 100644 --- a/azure/backend.go +++ b/azure/backend.go @@ -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, } } diff --git a/backend/backend.go b/backend/backend.go index eb0c3fcb8..45b460971 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -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) diff --git a/client/client.go b/client/client.go index 80f5e3d3e..88be54489 100644 --- a/client/client.go +++ b/client/client.go @@ -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, diff --git a/example/backend.go b/example/backend.go index 7fb82de87..be00e0c19 100644 --- a/example/backend.go +++ b/example/backend.go @@ -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 +}