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() { 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) return New(ctx)
}) })
} }
@ -53,11 +53,11 @@ func New(ctx context.Context) (backend.Service, error) {
func getAciAPIService(cgc containerinstance.ContainerGroupsClient, aciCtx store.AciContext) *aciAPIService { func getAciAPIService(cgc containerinstance.ContainerGroupsClient, aciCtx store.AciContext) *aciAPIService {
return &aciAPIService{ return &aciAPIService{
container: aciContainerService{ aciContainerService: aciContainerService{
containerGroupsClient: cgc, containerGroupsClient: cgc,
ctx: aciCtx, ctx: aciCtx,
}, },
compose: aciComposeService{ aciComposeService: aciComposeService{
containerGroupsClient: cgc, containerGroupsClient: cgc,
ctx: aciCtx, ctx: aciCtx,
}, },
@ -65,21 +65,21 @@ func getAciAPIService(cgc containerinstance.ContainerGroupsClient, aciCtx store.
} }
type aciAPIService struct { type aciAPIService struct {
container aciContainerService aciContainerService
compose aciComposeService aciComposeService
} }
func (a *aciAPIService) ContainerService() containers.Service { func (a *aciAPIService) ContainerService() containers.Service {
return &aciContainerService{ return &aciContainerService{
containerGroupsClient: a.container.containerGroupsClient, containerGroupsClient: a.aciContainerService.containerGroupsClient,
ctx: a.container.ctx, ctx: a.aciContainerService.ctx,
} }
} }
func (a *aciAPIService) ComposeService() compose.Service { func (a *aciAPIService) ComposeService() compose.Service {
return &aciComposeService{ return &aciComposeService{
containerGroupsClient: a.compose.containerGroupsClient, containerGroupsClient: a.aciComposeService.containerGroupsClient,
ctx: a.compose.ctx, ctx: a.aciComposeService.ctx,
} }
} }

View File

@ -17,7 +17,7 @@ var (
errTypeRegistered = errors.New("backend: already registered") errTypeRegistered = errors.New("backend: already registered")
) )
type initFunc func(context.Context) (interface{}, error) type initFunc func(context.Context) (Service, error)
type registeredBackend struct { type registeredBackend struct {
name string 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 // Get returns the backend registered for a particular type, it returns
// an error if there is no registered backends for the given type. // 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 { for _, b := range backends.r {
if b.backendType == backendType { if b.backendType == backendType {
return b.init(ctx) return b.init(ctx)

View File

@ -29,7 +29,6 @@ package client
import ( import (
"context" "context"
"errors"
"github.com/docker/api/backend" "github.com/docker/api/backend"
backendv1 "github.com/docker/api/backend/v1" backendv1 "github.com/docker/api/backend/v1"
@ -53,15 +52,11 @@ func New(ctx context.Context) (*Client, error) {
} }
contextType := s.GetType(cc) contextType := s.GetType(cc)
b, err := backend.Get(ctx, contextType) service, err := backend.Get(ctx, contextType)
if err != nil { if err != nil {
return nil, err return nil, err
} }
service, ok := b.(backend.Service)
if !ok {
return nil, errors.New("backend not found")
}
return &Client{ return &Client{
backendType: contextType, backendType: contextType,
bs: service, bs: service,

View File

@ -6,17 +6,31 @@ import (
"io" "io"
"github.com/docker/api/backend" "github.com/docker/api/backend"
"github.com/docker/api/compose"
"github.com/docker/api/containers" "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() { func init() {
backend.Register("example", "example", func(ctx context.Context) (interface{}, error) { backend.Register("example", "example", func(ctx context.Context) (backend.Service, error) {
return &containerService{}, nil return &apiService{}, nil
}) })
} }
type containerService struct{}
func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) { func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
return []containers.Container{ 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) fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
return nil 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
}