backend.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package local
  14. import (
  15. "context"
  16. "github.com/docker/cli/cli/command"
  17. "github.com/docker/compose-cli/api/backend"
  18. "github.com/docker/compose-cli/api/cloud"
  19. "github.com/docker/compose-cli/api/compose"
  20. apiconfig "github.com/docker/compose-cli/api/config"
  21. "github.com/docker/compose-cli/api/containers"
  22. apicontext "github.com/docker/compose-cli/api/context"
  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. local_compose "github.com/docker/compose-cli/local/compose"
  27. cliconfig "github.com/docker/cli/cli/config"
  28. )
  29. type local struct {
  30. containerService *containerService
  31. volumeService *volumeService
  32. composeService compose.Service
  33. }
  34. func init() {
  35. backend.Register("local", "local", service, cloud.NotImplementedCloudService)
  36. }
  37. func service(ctx context.Context) (backend.Service, error) {
  38. options := apicontext.CliOptions(ctx)
  39. config := apiconfig.Dir(ctx)
  40. configFile, err := cliconfig.Load(config)
  41. if err != nil {
  42. return nil, err
  43. }
  44. apiClient, err := command.NewAPIClientFromFlags(&options, configFile)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return &local{
  49. containerService: &containerService{apiClient},
  50. volumeService: &volumeService{apiClient},
  51. composeService: local_compose.NewComposeService(apiClient),
  52. }, nil
  53. }
  54. func (s *local) ContainerService() containers.Service {
  55. return s.containerService
  56. }
  57. func (s *local) ComposeService() compose.Service {
  58. return s.composeService
  59. }
  60. func (s *local) SecretsService() secrets.Service {
  61. return nil
  62. }
  63. func (s *local) VolumeService() volumes.Service {
  64. return s.volumeService
  65. }
  66. func (s *local) ResourceService() resources.Service {
  67. return nil
  68. }