-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
220 lines (189 loc) · 4.27 KB
/
configuration.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package configurationmanager
import (
"errors"
"fmt"
"reflect"
"strings"
"sync"
"github.com/andreasisnes/go-configuration-manager/modules"
)
var (
ErrKeyNotFound = errors.New("key not found")
ErrPointerNotPassed = errors.New("pointer not passed")
)
type Configuration interface {
List() map[string]interface{}
Get(key string, value any) any
Deconstruct() Configuration
Refresh() (isRefreshed bool)
}
type configuration struct {
refreshC chan modules.Module
quitC chan struct{}
waitgroup sync.WaitGroup
modules []modules.Module
options *Options
}
func newConfiguration(options *Options, sources []modules.Module) Configuration {
if options == nil {
options = NewDefaultOptions()
}
config := &configuration{
waitgroup: sync.WaitGroup{},
modules: sources,
options: options,
refreshC: make(chan modules.Module),
quitC: make(chan struct{}),
}
for _, source := range config.modules {
source.Load()
source.Connect(config.refreshC)
}
config.Refresh()
go config.autoRefresh()
return config
}
func (c *configuration) List() map[string]interface{} {
result := make(map[string]interface{})
for _, module := range c.modules {
for _, key := range module.GetKeys() {
moduleKey := key
if module.GetOptions().Delimiter != "" {
moduleKey = strings.ReplaceAll(key, module.GetOptions().Delimiter, c.options.Delimiter)
}
result[moduleKey] = c.Get(key, nil)
}
}
return result
}
// Get
func (c *configuration) Get(key string, out any) any {
for idx := range c.modules {
source := c.modules[len(c.modules)-1-idx]
if source.Exists(key) {
sourceKey := key
if source.GetOptions().Delimiter != "" {
sourceKey = strings.ReplaceAll(key, c.options.Delimiter, source.GetOptions().Delimiter)
}
value := source.Get(sourceKey)
if out == nil {
return value
}
return CastAndAssignValue(value, out)
}
}
return nil
}
// Refresh
func (c *configuration) Refresh() (successfullyRefreshed bool) {
defer func() {
if r := recover(); r != nil {
successfullyRefreshed = false
fmt.Println("Recovered. Error:\n", r)
} else {
successfullyRefreshed = true
}
}()
wg := sync.WaitGroup{}
for _, source := range c.modules {
wg.Add(1)
go func(sourceArg modules.Module) {
defer wg.Done()
sourceArg.Load()
}(source)
}
wg.Wait()
return successfullyRefreshed
}
// Deconstruct
func (c *configuration) Deconstruct() Configuration {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from error:\n", r)
}
}()
c.quitC <- struct{}{}
wg := sync.WaitGroup{}
for _, source := range c.modules {
wg.Add(1)
go func(sourceArg modules.Module) {
defer wg.Done()
sourceArg.Deconstruct()
}(source)
}
wg.Wait()
c.waitgroup.Wait()
return c
}
func (c *configuration) autoRefresh() {
c.waitgroup.Add(1)
defer c.waitgroup.Done()
for {
select {
case source := <-c.refreshC:
if source.GetOptions().ReloadOnChange {
source.Load()
}
if source.GetOptions().SentinelOptions != nil {
c.loadSentinel(source)
}
case <-c.quitC:
return
}
}
}
func (c *configuration) loadSentinel(source modules.Module) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Recovered from error:\n", r)
}
}()
key := source.GetOptions().SentinelOptions.Key
if reflect.DeepEqual(source.Get(key), source.GetRefreshedValue(key)) {
return
}
switch source.GetOptions().SentinelOptions.RefreshPolicy {
case modules.RefreshAll:
c.Refresh()
case modules.RefreshCurrent:
source.Load()
case modules.RefreshCurrentAndOver:
c.refreshCurrentAndAbove(source)
case modules.RefreshCurrentAndUnder:
c.refreshCurrentAndUnder(source)
}
}
func (c *configuration) refreshCurrentAndAbove(source modules.Module) {
wg := sync.WaitGroup{}
isAbove := false
for _, module := range c.modules {
if module == source {
isAbove = true
}
if isAbove {
wg.Add(1)
go func(sourceArg modules.Module) {
defer wg.Done()
sourceArg.Load()
}(source)
}
}
wg.Wait()
}
func (c *configuration) refreshCurrentAndUnder(source modules.Module) {
wg := sync.WaitGroup{}
isUnder := true
for _, s := range c.modules {
if isUnder {
wg.Add(1)
go func(sourceArg modules.Module) {
defer wg.Done()
sourceArg.Load()
}(source)
}
if s == source {
isUnder = false
}
}
wg.Wait()
}