backend.go 2.3 KB

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