backend.go 2.4 KB

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