-
Notifications
You must be signed in to change notification settings - Fork 0
/
environment.go
149 lines (110 loc) · 4.03 KB
/
environment.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package core
import (
"os"
"strings"
)
type Environment interface {
PropertyResolver
}
type ConfigurableEnvironment interface {
Environment
GetPropertySources() *PropertySources
GetSystemEnvironment() []string
GetTypeConverterService() TypeConverterService
}
type StandardEnvironment struct {
propertySources *PropertySources
converterService TypeConverterService
propertyResolver PropertyResolver
}
func NewStandardEnvironment() StandardEnvironment {
env := StandardEnvironment{
propertySources: NewPropertySources(),
converterService: NewDefaultTypeConverterService(),
}
env.propertyResolver = NewSimplePropertyResolver(env.propertySources)
return env
}
func (env StandardEnvironment) GetPropertySources() *PropertySources {
return env.propertySources
}
func (env StandardEnvironment) GetSystemEnvironment() []string {
return os.Environ()
}
func (env StandardEnvironment) ContainsProperty(name string) bool {
return env.propertyResolver.ContainsProperty(name)
}
func (env StandardEnvironment) GetProperty(name string, defaultValue string) interface{} {
return env.propertyResolver.GetProperty(name, defaultValue)
}
func (env StandardEnvironment) GetTypeConverterService() TypeConverterService {
return env.converterService
}
const ProcyonSystemEnvironmentPropertySource = "ProcyonSystemEnvironmentPropertySource"
type SystemEnvironmentPropertySource struct {
environmentProperties map[string]string
}
func NewSystemEnvironmentPropertySource() SystemEnvironmentPropertySource {
propertySource := SystemEnvironmentPropertySource{
environmentProperties: make(map[string]string, 0),
}
environmentProperties := os.Environ()
for _, property := range environmentProperties {
index := strings.Index(property, "=")
if index != -1 {
propertySource.environmentProperties[property[:index]] = property[index+1:]
}
}
return propertySource
}
func (propertySource SystemEnvironmentPropertySource) GetName() string {
return ProcyonSystemEnvironmentPropertySource
}
func (propertySource SystemEnvironmentPropertySource) GetSource() interface{} {
return propertySource.environmentProperties
}
func (propertySource SystemEnvironmentPropertySource) GetProperty(name string) interface{} {
actualPropertyName := propertySource.checkPropertyName(strings.ToLower(name))
if actualPropertyName != nil {
return propertySource.environmentProperties[actualPropertyName.(string)]
}
actualPropertyName = propertySource.checkPropertyName(strings.ToUpper(name))
if actualPropertyName != nil {
return propertySource.environmentProperties[actualPropertyName.(string)]
}
return nil
}
func (propertySource SystemEnvironmentPropertySource) ContainsProperty(name string) bool {
return propertySource.checkPropertyName(strings.ToUpper(name)) != nil || propertySource.checkPropertyName(strings.ToLower(name)) != nil
}
func (propertySource SystemEnvironmentPropertySource) GetPropertyNames() []string {
keys := make([]string, 0, len(propertySource.environmentProperties))
for key, _ := range propertySource.environmentProperties {
keys = append(keys, key)
}
return keys
}
func (propertySource SystemEnvironmentPropertySource) checkIfPresent(propertyName string) bool {
if _, ok := propertySource.environmentProperties[propertyName]; ok {
return true
}
return false
}
func (propertySource SystemEnvironmentPropertySource) checkPropertyName(propertyName string) interface{} {
if propertySource.checkIfPresent(propertyName) {
return propertyName
}
noHyphenPropertyName := strings.ReplaceAll(propertyName, "-", "_")
if propertyName != noHyphenPropertyName && propertySource.checkIfPresent(noHyphenPropertyName) {
return noHyphenPropertyName
}
noDotPropertyName := strings.ReplaceAll(propertyName, ".", "_")
if propertyName != noDotPropertyName && propertySource.checkIfPresent(noDotPropertyName) {
return noDotPropertyName
}
noHyphenAndNoDotName := strings.ReplaceAll(noDotPropertyName, "-", "_")
if noDotPropertyName != noHyphenAndNoDotName && propertySource.checkIfPresent(noHyphenAndNoDotName) {
return noHyphenAndNoDotName
}
return nil
}