backend.go 5.3 KB

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