backend.go 2.6 KB

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