1717package backend
1818
1919import (
20- "errors"
21- "fmt"
22-
23- "github.com/docker/compose/v2/pkg/api"
24- "github.com/sirupsen/logrus"
25-
26- "github.com/docker/compose-ecs/api/cloud"
27- "github.com/docker/compose-ecs/api/containers"
28- "github.com/docker/compose-ecs/api/resources"
2920 "github.com/docker/compose-ecs/api/secrets"
3021 "github.com/docker/compose-ecs/api/volumes"
22+ "github.com/docker/compose/v2/pkg/api"
3123)
3224
33- var (
34- errNoType = errors .New ("backend: no type" )
35- errNoName = errors .New ("backend: no name" )
36- errTypeRegistered = errors .New ("backend: already registered" )
37- )
38-
39- type initFunc func () (Service , error )
40- type getCloudServiceFunc func () (cloud.Service , error )
41-
42- type registeredBackend struct {
43- name string
44- backendType string
45- init initFunc
46- getCloudService getCloudServiceFunc
47- }
48-
49- var backends = struct {
50- r []* registeredBackend
51- }{}
52-
5325var instance Service
5426
5527// Current return the active backend instance
@@ -64,55 +36,7 @@ func WithBackend(s Service) {
6436
6537// Service aggregates the service interfaces
6638type Service interface {
67- ContainerService () containers.Service
6839 ComposeService () api.Service
69- ResourceService () resources.Service
7040 SecretsService () secrets.Service
7141 VolumeService () volumes.Service
7242}
73-
74- // Register adds a typed backend to the registry
75- func Register (name string , backendType string , init initFunc , getCoudService getCloudServiceFunc ) {
76- if name == "" {
77- logrus .Fatal (errNoName )
78- }
79- if backendType == "" {
80- logrus .Fatal (errNoType )
81- }
82- for _ , b := range backends .r {
83- if b .backendType == backendType {
84- logrus .Fatal (errTypeRegistered )
85- }
86- }
87-
88- backends .r = append (backends .r , & registeredBackend {
89- name ,
90- backendType ,
91- init ,
92- getCoudService ,
93- })
94- }
95-
96- // Get returns the backend registered for a particular type, it returns
97- // an error if there is no registered backends for the given type.
98- func Get (backendType string ) (Service , error ) {
99- for _ , b := range backends .r {
100- if b .backendType == backendType {
101- return b .init ()
102- }
103- }
104-
105- return nil , api .ErrNotFound
106- }
107-
108- // GetCloudService returns the backend registered for a particular type, it returns
109- // an error if there is no registered backends for the given type.
110- func GetCloudService (backendType string ) (cloud.Service , error ) {
111- for _ , b := range backends .r {
112- if b .backendType == backendType {
113- return b .getCloudService ()
114- }
115- }
116-
117- return nil , fmt .Errorf ("backend not found for backend type %s" , backendType )
118- }
0 commit comments