backend.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. "os"
  16. "github.com/docker/cli/cli/command"
  17. cliconfig "github.com/docker/cli/cli/config"
  18. cliflags "github.com/docker/cli/cli/flags"
  19. "github.com/docker/docker/client"
  20. "github.com/docker/compose-cli/api/backend"
  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. cliopts "github.com/docker/compose-cli/cli/options"
  27. local_compose "github.com/docker/compose-cli/local/compose"
  28. )
  29. type local struct {
  30. containerService *containerService
  31. volumeService *volumeService
  32. composeService compose.Service
  33. }
  34. // NewService build a backend for "local" context, using Docker API client
  35. func NewService(apiClient client.APIClient) backend.Service {
  36. file := cliconfig.LoadDefaultConfigFile(os.Stderr)
  37. return &local{
  38. containerService: &containerService{apiClient},
  39. volumeService: &volumeService{apiClient},
  40. composeService: local_compose.NewComposeService(apiClient, file),
  41. }
  42. }
  43. // GetLocalBackend initialize local backend
  44. func GetLocalBackend(configDir string, opts cliopts.GlobalOpts) (backend.Service, error) {
  45. configFile, err := cliconfig.Load(configDir)
  46. if err != nil {
  47. return nil, err
  48. }
  49. options := cliflags.CommonOptions{
  50. Context: opts.Context,
  51. Debug: opts.Debug,
  52. Hosts: opts.Hosts,
  53. LogLevel: opts.LogLevel,
  54. }
  55. if opts.TLSVerify {
  56. options.TLS = opts.TLS
  57. options.TLSVerify = opts.TLSVerify
  58. options.TLSOptions = opts.TLSOptions
  59. }
  60. apiClient, err := command.NewAPIClientFromFlags(&options, configFile)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return NewService(apiClient), nil
  65. }
  66. func (s *local) ContainerService() containers.Service {
  67. return s.containerService
  68. }
  69. func (s *local) ComposeService() compose.Service {
  70. return s.composeService
  71. }
  72. func (s *local) SecretsService() secrets.Service {
  73. return nil
  74. }
  75. func (s *local) VolumeService() volumes.Service {
  76. return s.volumeService
  77. }
  78. func (s *local) ResourceService() resources.Service {
  79. return nil
  80. }