/
drive.go
91 lines (78 loc) · 2.28 KB
/
drive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package service
import (
"strings"
"github.com/helderfarias/go-config-server/internal/domain"
"github.com/sirupsen/logrus"
)
type DriveNativeFactory interface {
Build() *domain.BuildSource
}
type emptyDriveNative struct {
}
func newDriveNative(cfg domain.EnvConfig) DriveNativeFactory {
actives := strings.Split(cfg.Cloud.Spring.Profiles.Active, ",")
if isDriveInvalid(actives) {
logrus.Warnf("Profile is not defined: %s", actives)
return &emptyDriveNative{}
}
if len(actives) == 0 {
return &gitDriveNative{
source: cfg.Cloud.Spring.Cloud.Config.Server.Git,
application: cfg.Application,
profile: cfg.Profile,
label: cfg.Label,
cryptService: newCryptService(cfg),
}
}
return createComposeDrive(actives, cfg)
}
func (e *emptyDriveNative) Build() *domain.BuildSource {
return domain.NewBuildSource()
}
func isDriveInvalid(actives []string) bool {
for _, s := range actives {
if strings.TrimSpace(s) == strings.TrimSpace("native") ||
strings.TrimSpace(s) == strings.TrimSpace("git") ||
strings.TrimSpace(s) == strings.TrimSpace("vault") {
return false
}
}
return true
}
func createComposeDrive(actives []string, cfg domain.EnvConfig) DriveNativeFactory {
compose := &composeDriveNative{
targets: []DriveNativeFactory{},
}
for i := len(actives) - 1; i >= 0; i-- {
if strings.TrimSpace(actives[i]) == "vault" {
compose.Add(&vaultDriveNative{
index: i,
source: cfg.Cloud.Spring.Cloud.Config.Server.Vault,
application: cfg.Application,
profile: cfg.Profile,
label: cfg.Label,
vaultToken: cfg.VaultToken,
cryptService: newCryptService(cfg),
})
} else if strings.TrimSpace(actives[i]) == "git" {
compose.Add(&gitDriveNative{
index: i,
source: cfg.Cloud.Spring.Cloud.Config.Server.Git,
application: cfg.Application,
profile: cfg.Profile,
label: cfg.Label,
cryptService: newCryptService(cfg),
})
} else if strings.TrimSpace(actives[i]) == "native" {
compose.Add(&fileDriveNative{
index: i,
source: cfg.Cloud.Spring.Cloud.Config.Server.Native,
application: cfg.Application,
profile: cfg.Profile,
label: cfg.Label,
cryptService: newCryptService(cfg),
})
}
}
return compose
}