2020-04-29 19:57:53 +02:00
|
|
|
package example
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-16 12:13:51 +02:00
|
|
|
"errors"
|
2020-05-01 15:28:44 +02:00
|
|
|
"fmt"
|
2020-05-03 13:35:25 +02:00
|
|
|
"io"
|
2020-04-29 19:57:53 +02:00
|
|
|
|
2020-05-14 22:54:57 +02:00
|
|
|
"github.com/docker/api/context/cloud"
|
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/docker/api/backend"
|
2020-05-05 17:14:26 +02:00
|
|
|
"github.com/docker/api/compose"
|
2020-04-29 19:57:53 +02:00
|
|
|
"github.com/docker/api/containers"
|
|
|
|
)
|
|
|
|
|
2020-05-05 17:14:26 +02:00
|
|
|
type apiService struct {
|
|
|
|
containerService
|
|
|
|
composeService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *apiService) ContainerService() containers.Service {
|
|
|
|
return &a.containerService
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *apiService) ComposeService() compose.Service {
|
|
|
|
return &a.composeService
|
|
|
|
}
|
2020-04-29 19:57:53 +02:00
|
|
|
|
2020-05-12 13:37:28 +02:00
|
|
|
func (a *apiService) CloudService() cloud.Service {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:57:53 +02:00
|
|
|
func init() {
|
2020-05-05 17:14:26 +02:00
|
|
|
backend.Register("example", "example", func(ctx context.Context) (backend.Service, error) {
|
|
|
|
return &apiService{}, nil
|
2020-04-29 19:57:53 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:14:26 +02:00
|
|
|
type containerService struct{}
|
|
|
|
|
2020-05-18 14:16:32 +02:00
|
|
|
func (cs *containerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
|
|
|
|
result := []containers.Container{
|
2020-04-29 19:57:53 +02:00
|
|
|
{
|
|
|
|
ID: "id",
|
|
|
|
Image: "nginx",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "1234",
|
|
|
|
Image: "alpine",
|
|
|
|
},
|
2020-05-18 14:16:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if all {
|
|
|
|
result = append(result, containers.Container{
|
|
|
|
ID: "stopped",
|
|
|
|
Image: "nginx",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2020-04-29 19:57:53 +02:00
|
|
|
}
|
2020-05-01 15:28:44 +02:00
|
|
|
|
|
|
|
func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
|
|
|
|
fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-03 13:35:25 +02:00
|
|
|
|
2020-05-18 12:02:04 +02:00
|
|
|
func (cs *containerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
|
2020-05-16 12:13:51 +02:00
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2020-05-03 13:35:25 +02:00
|
|
|
func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
|
|
|
|
fmt.Printf("Executing command %q on container %q", command, name)
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-03 13:41:45 +02:00
|
|
|
|
2020-05-04 16:38:02 +02:00
|
|
|
func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
|
|
|
|
fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
|
2020-05-03 13:41:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-05 17:14:26 +02:00
|
|
|
|
2020-05-10 22:37:28 +02:00
|
|
|
func (cs *containerService) Delete(ctx context.Context, id string, force bool) error {
|
|
|
|
fmt.Printf("Deleting container %q with force = %t\n", id, force)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-05 17:14:26 +02:00
|
|
|
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
|
|
|
|
}
|