backend.go 2.1 KB

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