This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
243 lines (183 loc) · 4.17 KB
/
config.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package confighub
import (
"time"
"encoding/json"
"sync"
"fmt"
"net/http"
)
type ConfigProvider interface {
GetProperty(string) Value
GetFile(string) *File
AddWatch(string, WatchFunc)
}
type ConfigHubClient struct {
mu sync.Mutex
options *ConfigOptions
watches map[string][]WatchFunc
Account string `json:"account"`
GenerationTime time.Time `json:"generated_on"`
Repo string `json:"repo"`
Context string `json:"context"`
PropertiesB json.RawMessage `json:"properties,omitempty"`
Properties *Properties `json:"-"`
FilesB json.RawMessage `json:"files,omitempty"`
Files *Files `json:"-"`
client *http.Client
}
func NewConfigHubClient(options *ConfigOptions) *ConfigHubClient {
return &ConfigHubClient{
options: options,
watches: make(map[string][]WatchFunc),
}
}
func (c *ConfigHubClient) UnmarshalJSON(data []byte) (err error) {
var m map[string]json.RawMessage
err = json.Unmarshal(data, &m)
if err != nil {
return
}
if accI, ok := m["account"]; ok {
err = json.Unmarshal(accI, &c.Account)
if err != nil {
return
}
}
if genI, ok := m["generated_on"]; ok {
// 01/21/2018 23:02:47
c.GenerationTime, err = time.Parse("01/02/2006 15:04:05", string(genI))
if err != nil {
return
}
}
if repoI, ok := m["repo"]; ok {
err = json.Unmarshal(repoI, &c.Repo)
if err != nil {
return
}
}
if ctxI, ok := m["context"]; ok {
err = json.Unmarshal(ctxI, &c.Context)
if err != nil {
return
}
}
c.Files = &Files{
cfg: c,
entries: make(map[string]*File),
}
if fI, ok := m["files"]; ok {
var m map[string]json.RawMessage
err = json.Unmarshal(fI, &m)
if err != nil {
return
}
for k, v := range m {
var file File
if err = json.Unmarshal(v, &file); err != nil {
return
}
file.Name = k
c.Files.entries[k] = &file
}
}
c.Properties = &Properties{
cfg: c,
entries: make(map[string]*Property),
}
if pI, ok := m["properties"]; ok {
var m map[string]json.RawMessage
err = json.Unmarshal(pI, &m)
if err != nil {
return
}
for k, v := range m {
var property = Property{
cfg: c,
}
if err = json.Unmarshal(v, &property); err != nil {
return
}
property.cfg = c
property.key = k
c.Properties.entries[k] = &property
}
}
return nil
}
func (c *ConfigHubClient) GetProperty(key string) (v Value) {
if p, ok := c.Properties.entries[key]; ok {
return p.value
}
return &NilValue{}
}
func (c *ConfigHubClient) GetFile(name string) (f *File) {
if c.Files == nil {
return nil
}
if f, ok := c.Files.entries[name]; ok {
return f
}
return nil
}
func (c *ConfigHubClient) fetch() (result interface{}, err error) {
if c.client == nil {
c.client = &http.Client{}
}
var req *http.Request
req, err = http.NewRequest(http.MethodGet, fmt.Sprintf("%s/rest/pull/%s/api", c.options.Host, c.options.Repo), nil)
req.Header.Set("Context", c.options.Context)
return c.client.Do(req)
}
type WatchFunc func(val Value) error
func (c *ConfigHubClient) AddWatch(key string, watch WatchFunc) {
c.mu.Lock()
defer c.mu.Unlock()
if watchers, ok := c.watches[key]; ok {
watchers = append(watchers, watch)
} else {
watchers = []WatchFunc{watch}
}
}
type ConfigOption func(*ConfigOptions)
func DefaultOptions() *ConfigOptions {
return &ConfigOptions{
Host: "127.0.0.1",
Port: 8080,
Token: "secret",
Repo: "default",
Context: "",
}
}
type ConfigOptions struct {
Host string `json:"host"`
Port int64 `json:"port"`
Token string `json:"token"`
Repo string `json:"repo"`
Context string `json:"context"`
}
func Host(host string) ConfigOption {
return func(config *ConfigOptions) {
config.Host = host
}
}
func Port(port int64) ConfigOption {
return func(config *ConfigOptions) {
config.Port = port
}
}
func Token(token string) ConfigOption {
return func(config *ConfigOptions) {
config.Token = token
}
}
func Repo(repo string) ConfigOption {
return func(config *ConfigOptions) {
config.Repo = repo
}
}
func Context(context string) ConfigOption {
return func(config *ConfigOptions) {
config.Context = context
}
}