backend.go 2.4 KB

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