backend.go 2.9 KB

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