backend.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package example
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "github.com/docker/api/backend"
  7. "github.com/docker/api/compose"
  8. "github.com/docker/api/containers"
  9. )
  10. type apiService struct {
  11. containerService
  12. composeService
  13. }
  14. func (a *apiService) ContainerService() containers.Service {
  15. return &a.containerService
  16. }
  17. func (a *apiService) ComposeService() compose.Service {
  18. return &a.composeService
  19. }
  20. func init() {
  21. backend.Register("example", "example", func(ctx context.Context) (backend.Service, error) {
  22. return &apiService{}, nil
  23. })
  24. }
  25. type containerService struct{}
  26. func (cs *containerService) List(ctx context.Context) ([]containers.Container, error) {
  27. return []containers.Container{
  28. {
  29. ID: "id",
  30. Image: "nginx",
  31. },
  32. {
  33. ID: "1234",
  34. Image: "alpine",
  35. },
  36. }, nil
  37. }
  38. func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  39. fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
  40. return nil
  41. }
  42. func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  43. fmt.Printf("Executing command %q on container %q", command, name)
  44. return nil
  45. }
  46. func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  47. fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
  48. return nil
  49. }
  50. type composeService struct{}
  51. func (cs *composeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  52. prj, err := compose.ProjectFromOptions(&opts)
  53. if err != nil {
  54. return err
  55. }
  56. fmt.Printf("Up command on project %q", prj.Name)
  57. return nil
  58. }
  59. func (cs *composeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  60. prj, err := compose.ProjectFromOptions(&opts)
  61. if err != nil {
  62. return err
  63. }
  64. fmt.Printf("Down command on project %q", prj.Name)
  65. return nil
  66. }