1
0

backend.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // +build example
  2. /*
  3. Copyright 2020 Docker, Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package example
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "github.com/compose-spec/compose-go/cli"
  21. "github.com/docker/api/context/cloud"
  22. "github.com/docker/api/backend"
  23. "github.com/docker/api/compose"
  24. "github.com/docker/api/containers"
  25. )
  26. type apiService struct {
  27. containerService
  28. composeService
  29. }
  30. func (a *apiService) ContainerService() containers.Service {
  31. return &a.containerService
  32. }
  33. func (a *apiService) ComposeService() compose.Service {
  34. return &a.composeService
  35. }
  36. func init() {
  37. backend.Register("example", "example", service, cloud.NotImplementedCloudService)
  38. }
  39. func service(ctx context.Context) (backend.Service, error) {
  40. return &apiService{}, nil
  41. }
  42. type containerService struct{}
  43. func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
  44. return containers.Container{
  45. ID: "id",
  46. Image: "nginx",
  47. }, nil
  48. }
  49. func (cs *containerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
  50. result := []containers.Container{
  51. {
  52. ID: "id",
  53. Image: "nginx",
  54. },
  55. {
  56. ID: "1234",
  57. Image: "alpine",
  58. },
  59. }
  60. if all {
  61. result = append(result, containers.Container{
  62. ID: "stopped",
  63. Image: "nginx",
  64. })
  65. }
  66. return result, nil
  67. }
  68. func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  69. fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
  70. return nil
  71. }
  72. func (cs *containerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  73. return errors.New("not implemented")
  74. }
  75. func (cs *containerService) Exec(ctx context.Context, name string, command string, reader io.Reader, writer io.Writer) error {
  76. fmt.Printf("Executing command %q on container %q", command, name)
  77. return nil
  78. }
  79. func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  80. fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
  81. return nil
  82. }
  83. func (cs *containerService) Delete(ctx context.Context, id string, force bool) error {
  84. fmt.Printf("Deleting container %q with force = %t\n", id, force)
  85. return nil
  86. }
  87. type composeService struct{}
  88. func (cs *composeService) Up(ctx context.Context, opts cli.ProjectOptions) error {
  89. prj, err := cli.ProjectFromOptions(&opts)
  90. if err != nil {
  91. return err
  92. }
  93. fmt.Printf("Up command on project %q", prj.Name)
  94. return nil
  95. }
  96. func (cs *composeService) Down(ctx context.Context, opts cli.ProjectOptions) error {
  97. prj, err := cli.ProjectFromOptions(&opts)
  98. if err != nil {
  99. return err
  100. }
  101. fmt.Printf("Down command on project %q", prj.Name)
  102. return nil
  103. }