11package backend
22
33import (
4- "fmt "
4+ "strings "
55
66 "github.com/devstream-io/devstream/internal/pkg/backend/k8s"
77 "github.com/devstream-io/devstream/internal/pkg/backend/local"
88 "github.com/devstream-io/devstream/internal/pkg/backend/s3"
9+ "github.com/devstream-io/devstream/internal/pkg/backend/types"
910 "github.com/devstream-io/devstream/internal/pkg/configmanager"
1011 "github.com/devstream-io/devstream/pkg/util/log"
1112)
1213
13- type Type string
14-
15- const (
16- Local Type = "local"
17- S3 Type = "s3"
18- K8s Type = "k8s"
19- )
20-
2114// Backend is used to persist data, it can be local file/etcd/s3/k8s...
2215type Backend interface {
2316 // Read is used to read data from persistent storage.
@@ -28,24 +21,24 @@ type Backend interface {
2821
2922// GetBackend will return a Backend by the given name.
3023func GetBackend (state configmanager.State ) (Backend , error ) {
31- typeName := Type (state .Backend )
32- switch typeName {
33- case Local :
24+ typeName := types . Type (state .Backend )
25+ switch {
26+ case types . Local == typeName :
3427 log .Infof ("Using local backend. State file: %s." , state .Options .StateFile )
35- return local .NewLocal (state .Options .StateFile ), nil
36- case S3 :
28+ return local .NewLocal (state .Options .StateFile )
29+ case types . S3 == typeName :
3730 log .Infof ("Using s3 backend. Bucket: %s, region: %s, key: %s." , state .Options .Bucket , state .Options .Region , state .Options .Key )
3831 return s3 .NewS3Backend (
3932 state .Options .Bucket ,
4033 state .Options .Region ,
41- state .Options .Key ,
42- ), nil
43- case K8s :
34+ state .Options .Key )
35+ case strings . ToLower ( state . Backend ) == types . K8s . String () ||
36+ strings . ToLower ( state . Backend ) == types . K8sAlis . String () :
4437 log .Infof ("Using configmap backend. Namespace: %s, ConfigMap name: %s." , state .Options .Namespace , state .Options .ConfigMap )
4538 return k8s .NewBackend (
4639 state .Options .Namespace ,
4740 state .Options .ConfigMap )
4841 default :
49- return nil , fmt . Errorf ( "the backend type < %s > is illegal" , typeName )
42+ return nil , types . NewInvalidBackendErr ( state . Backend )
5043 }
5144}
0 commit comments