backend.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. func (cs *containerService) Delete(ctx context.Context, id string, force bool) error {
  51. fmt.Printf("Deleting container %q with force = %t\n", id, force)
  52. return nil
  53. }
  54. type composeService struct{}
  55. func (cs *composeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  56. prj, err := compose.ProjectFromOptions(&opts)
  57. if err != nil {
  58. return err
  59. }
  60. fmt.Printf("Up command on project %q", prj.Name)
  61. return nil
  62. }
  63. func (cs *composeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  64. prj, err := compose.ProjectFromOptions(&opts)
  65. if err != nil {
  66. return err
  67. }
  68. fmt.Printf("Down command on project %q", prj.Name)
  69. return nil
  70. }