backend.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 backend
  14. import (
  15. "errors"
  16. "fmt"
  17. "github.com/sirupsen/logrus"
  18. "github.com/docker/compose-cli/api/cloud"
  19. "github.com/docker/compose-cli/api/compose"
  20. "github.com/docker/compose-cli/api/containers"
  21. "github.com/docker/compose-cli/api/errdefs"
  22. "github.com/docker/compose-cli/api/resources"
  23. "github.com/docker/compose-cli/api/secrets"
  24. "github.com/docker/compose-cli/api/volumes"
  25. )
  26. var (
  27. errNoType = errors.New("backend: no type")
  28. errNoName = errors.New("backend: no name")
  29. errTypeRegistered = errors.New("backend: already registered")
  30. )
  31. type initFunc func() (Service, error)
  32. type getCloudServiceFunc func() (cloud.Service, error)
  33. type registeredBackend struct {
  34. name string
  35. backendType string
  36. init initFunc
  37. getCloudService getCloudServiceFunc
  38. }
  39. var backends = struct {
  40. r []*registeredBackend
  41. }{}
  42. var instance Service
  43. // Current return the active backend instance
  44. func Current() Service {
  45. return instance
  46. }
  47. // WithBackend set the active backend instance
  48. func WithBackend(s Service) {
  49. instance = s
  50. }
  51. // Service aggregates the service interfaces
  52. type Service interface {
  53. ContainerService() containers.Service
  54. ComposeService() compose.Service
  55. ResourceService() resources.Service
  56. SecretsService() secrets.Service
  57. VolumeService() volumes.Service
  58. }
  59. // Register adds a typed backend to the registry
  60. func Register(name string, backendType string, init initFunc, getCoudService getCloudServiceFunc) {
  61. if name == "" {
  62. logrus.Fatal(errNoName)
  63. }
  64. if backendType == "" {
  65. logrus.Fatal(errNoType)
  66. }
  67. for _, b := range backends.r {
  68. if b.backendType == backendType {
  69. logrus.Fatal(errTypeRegistered)
  70. }
  71. }
  72. backends.r = append(backends.r, &registeredBackend{
  73. name,
  74. backendType,
  75. init,
  76. getCoudService,
  77. })
  78. }
  79. // Get returns the backend registered for a particular type, it returns
  80. // an error if there is no registered backends for the given type.
  81. func Get(backendType string) (Service, error) {
  82. for _, b := range backends.r {
  83. if b.backendType == backendType {
  84. return b.init()
  85. }
  86. }
  87. return nil, errdefs.ErrNotFound
  88. }
  89. // GetCloudService returns the backend registered for a particular type, it returns
  90. // an error if there is no registered backends for the given type.
  91. func GetCloudService(backendType string) (cloud.Service, error) {
  92. for _, b := range backends.r {
  93. if b.backendType == backendType {
  94. return b.getCloudService()
  95. }
  96. }
  97. return nil, fmt.Errorf("backend not found for backend type %s", backendType)
  98. }