backend.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 init() {
  23. backend.Register("example", "example", service, cloud.NotImplementedCloudService)
  24. }
  25. func service(ctx context.Context) (backend.Service, error) {
  26. return &apiService{}, nil
  27. }
  28. type containerService struct{}
  29. func (cs *containerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
  30. result := []containers.Container{
  31. {
  32. ID: "id",
  33. Image: "nginx",
  34. },
  35. {
  36. ID: "1234",
  37. Image: "alpine",
  38. },
  39. }
  40. if all {
  41. result = append(result, containers.Container{
  42. ID: "stopped",
  43. Image: "nginx",
  44. })
  45. }
  46. return result, nil
  47. }
  48. func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  49. fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
  50. return nil
  51. }
  52. func (cs *containerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  53. return errors.New("not implemented")
  54. }
  55. func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  56. fmt.Printf("Executing command %q on container %q", command, name)
  57. return nil
  58. }
  59. func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  60. fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
  61. return nil
  62. }
  63. func (cs *containerService) Delete(ctx context.Context, id string, force bool) error {
  64. fmt.Printf("Deleting container %q with force = %t\n", id, force)
  65. return nil
  66. }
  67. type composeService struct{}
  68. func (cs *composeService) Up(ctx context.Context, opts compose.ProjectOptions) error {
  69. prj, err := compose.ProjectFromOptions(&opts)
  70. if err != nil {
  71. return err
  72. }
  73. fmt.Printf("Up command on project %q", prj.Name)
  74. return nil
  75. }
  76. func (cs *composeService) Down(ctx context.Context, opts compose.ProjectOptions) error {
  77. prj, err := compose.ProjectFromOptions(&opts)
  78. if err != nil {
  79. return err
  80. }
  81. fmt.Printf("Down command on project %q", prj.Name)
  82. return nil
  83. }