backend.go 2.7 KB

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