-
Notifications
You must be signed in to change notification settings - Fork 0
/
configurer.go
112 lines (88 loc) · 2.73 KB
/
configurer.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package procyon
import (
"codnect.io/procyon/runtime"
"codnect.io/procyon/runtime/config"
"codnect.io/procyon/runtime/property"
"context"
"strings"
)
type configContextConfigurer struct {
loaders []property.SourceLoader
importer *config.Importer
}
func newConfigContextConfigurer(loaders []property.SourceLoader, importer *config.Importer) *configContextConfigurer {
return &configContextConfigurer{
loaders: loaders,
importer: importer,
}
}
func (c *configContextConfigurer) ConfigureContext(ctx runtime.Context) error {
return c.importConfig(ctx.Environment())
}
func (c *configContextConfigurer) importConfig(environment runtime.Environment) error {
defaultConfigs, err := c.importer.Import(context.Background(), "resources", environment.DefaultProfiles())
if err != nil {
return err
}
sources := property.NewSources()
for _, defaultConfig := range defaultConfigs {
sources.AddLast(defaultConfig.PropertySource())
}
activeProfiles := environment.ActiveProfiles()
if len(activeProfiles) == 0 {
resolver := property.NewSourcesResolver(sources)
value, ok := resolver.Property("procyon.profiles.active")
if ok {
activeProfiles = strings.Split(strings.TrimSpace(value.(string)), ",")
}
}
if len(activeProfiles) != 0 {
err = environment.SetActiveProfiles(activeProfiles...)
if err != nil {
return err
}
err = c.loadActiveProfiles(environment, sources, activeProfiles)
if err != nil {
return err
}
}
c.mergeSources(environment, sources)
return nil
}
func (c *configContextConfigurer) loadActiveProfiles(environment runtime.Environment, sourceList *property.Sources, activeProfiles []string) error {
configs, err := c.importer.Import(context.Background(), "config", activeProfiles)
if err != nil {
return err
}
for _, cfg := range configs {
propertySource := cfg.PropertySource()
sourceList.AddFirst(propertySource)
err = c.activateIncludeProfiles(environment, sourceList, propertySource)
if err != nil {
return err
}
}
return nil
}
func (c *configContextConfigurer) activateIncludeProfiles(environment runtime.Environment, sourceList *property.Sources, source property.Source) error {
value, ok := source.Property("procyon.profiles.include")
if ok {
profiles := strings.Split(strings.TrimSpace(value.(string)), ",")
for _, profile := range profiles {
err := environment.AddActiveProfile(strings.TrimSpace(profile))
if err != nil {
return err
}
}
err := c.loadActiveProfiles(environment, sourceList, profiles)
if err != nil {
return err
}
}
return nil
}
func (c *configContextConfigurer) mergeSources(environment runtime.Environment, sourceList *property.Sources) {
for _, source := range sourceList.ToSlice() {
environment.PropertySources().AddLast(source)
}
}