Merge pull request #46 from ulyssessouza/refactor-example-backend

Refactor example/backend
This commit is contained in:
Ulysses Souza 2020-05-05 17:23:46 +02:00 committed by GitHub
commit 1be5ec602e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 20 deletions

View File

@ -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,
}
}

View File

@ -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)

View File

@ -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,

View File

@ -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
}