backend.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // +build example
  2. /*
  3. Copyright 2020 Docker Compose CLI authors
  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/types"
  21. "github.com/docker/compose-cli/api/compose"
  22. "github.com/docker/compose-cli/api/containers"
  23. "github.com/docker/compose-cli/api/resources"
  24. "github.com/docker/compose-cli/api/secrets"
  25. "github.com/docker/compose-cli/api/volumes"
  26. "github.com/docker/compose-cli/backend"
  27. "github.com/docker/compose-cli/context/cloud"
  28. "github.com/docker/compose-cli/errdefs"
  29. )
  30. type apiService struct {
  31. containerService
  32. composeService
  33. }
  34. func (a *apiService) ContainerService() containers.Service {
  35. return &a.containerService
  36. }
  37. func (a *apiService) ComposeService() compose.Service {
  38. return &a.composeService
  39. }
  40. func (a *apiService) SecretsService() secrets.Service {
  41. return nil
  42. }
  43. func (a *apiService) VolumeService() volumes.Service {
  44. return nil
  45. }
  46. func (a *apiService) ResourceService() resources.Service {
  47. return nil
  48. }
  49. func init() {
  50. backend.Register("example", "example", service, cloud.NotImplementedCloudService)
  51. }
  52. func service(ctx context.Context) (backend.Service, error) {
  53. return &apiService{}, nil
  54. }
  55. type containerService struct{}
  56. func (cs *containerService) Inspect(ctx context.Context, id string) (containers.Container, error) {
  57. return containers.Container{
  58. ID: "id",
  59. Image: "nginx",
  60. Platform: "Linux",
  61. HostConfig: &containers.HostConfig{
  62. RestartPolicy: "none",
  63. },
  64. }, nil
  65. }
  66. func (cs *containerService) List(ctx context.Context, all bool) ([]containers.Container, error) {
  67. result := []containers.Container{
  68. {
  69. ID: "id",
  70. Image: "nginx",
  71. },
  72. {
  73. ID: "1234",
  74. Image: "alpine",
  75. },
  76. }
  77. if all {
  78. result = append(result, containers.Container{
  79. ID: "stopped",
  80. Image: "nginx",
  81. })
  82. }
  83. return result, nil
  84. }
  85. func (cs *containerService) Run(ctx context.Context, r containers.ContainerConfig) error {
  86. fmt.Printf("Running container %q with name %q\n", r.Image, r.ID)
  87. return nil
  88. }
  89. func (cs *containerService) Start(ctx context.Context, containerID string) error {
  90. return errors.New("not implemented")
  91. }
  92. func (cs *containerService) Stop(ctx context.Context, containerName string, timeout *uint32) error {
  93. return errors.New("not implemented")
  94. }
  95. func (cs *containerService) Kill(ctx context.Context, containerName string, signal string) error {
  96. return errors.New("not implemented")
  97. }
  98. func (cs *containerService) Exec(ctx context.Context, name string, request containers.ExecRequest) error {
  99. fmt.Printf("Executing command %q on container %q", request.Command, name)
  100. return nil
  101. }
  102. func (cs *containerService) Logs(ctx context.Context, containerName string, request containers.LogsRequest) error {
  103. fmt.Fprintf(request.Writer, "Following logs for container %q", containerName)
  104. return nil
  105. }
  106. func (cs *containerService) Delete(ctx context.Context, id string, request containers.DeleteRequest) error {
  107. fmt.Printf("Deleting container %q with force = %t\n", id, request.Force)
  108. return nil
  109. }
  110. type composeService struct{}
  111. func (cs *composeService) Build(ctx context.Context, project *types.Project) error {
  112. fmt.Printf("Build command on project %q", project.Name)
  113. return nil
  114. }
  115. func (cs *composeService) Up(ctx context.Context, project *types.Project, detach bool) error {
  116. fmt.Printf("Up command on project %q", project.Name)
  117. return nil
  118. }
  119. func (cs *composeService) Down(ctx context.Context, project string) error {
  120. fmt.Printf("Down command on project %q", project)
  121. return nil
  122. }
  123. func (cs *composeService) Ps(ctx context.Context, project string) ([]compose.ServiceStatus, error) {
  124. return nil, errdefs.ErrNotImplemented
  125. }
  126. func (cs *composeService) List(ctx context.Context, project string) ([]compose.Stack, error) {
  127. return nil, errdefs.ErrNotImplemented
  128. }
  129. func (cs *composeService) Logs(ctx context.Context, project string, w io.Writer) error {
  130. return errdefs.ErrNotImplemented
  131. }
  132. func (cs *composeService) Convert(ctx context.Context, project *types.Project, format string) ([]byte, error) {
  133. return nil, errdefs.ErrNotImplemented
  134. }