forked from kelseyhightower/confd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
169 lines (153 loc) · 3.63 KB
/
client.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
package file
import (
"fmt"
"io/ioutil"
"path"
"strconv"
"strings"
"github.com/coder1966/confd/log"
util "github.com/coder1966/confd/util"
"github.com/fsnotify/fsnotify"
"gopkg.in/yaml.v2"
)
var replacer = strings.NewReplacer("/", "_")
// Client provides a shell for the yaml client
type Client struct {
filepath []string
filter string
}
type ResultError struct {
response uint64
err error
}
func NewFileClient(filepath []string, filter string) (*Client, error) {
return &Client{filepath: filepath, filter: filter}, nil
}
func readFile(path string, vars map[string]string) error {
yamlMap := make(map[interface{}]interface{})
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
err = yaml.Unmarshal(data, &yamlMap)
if err != nil {
return err
}
err = nodeWalk(yamlMap, "/", vars)
if err != nil {
return err
}
return nil
}
func (c *Client) GetValues(keys []string) (map[string]string, error) {
vars := make(map[string]string)
var filePaths []string
for _, path := range c.filepath {
p, err := util.RecursiveFilesLookup(path, c.filter)
if err != nil {
return nil, err
}
filePaths = append(filePaths, p...)
}
for _, path := range filePaths {
err := readFile(path, vars)
if err != nil {
return nil, err
}
}
VarsLoop:
for k, _ := range vars {
for _, key := range keys {
if strings.HasPrefix(k, key) {
continue VarsLoop
}
}
delete(vars, k)
}
log.Debug(fmt.Sprintf("Key Map: %#v", vars))
return vars, nil
}
// nodeWalk recursively descends nodes, updating vars.
func nodeWalk(node interface{}, key string, vars map[string]string) error {
switch node.(type) {
case []interface{}:
for i, j := range node.([]interface{}) {
key := path.Join(key, strconv.Itoa(i))
nodeWalk(j, key, vars)
}
case map[interface{}]interface{}:
for k, v := range node.(map[interface{}]interface{}) {
key := path.Join(key, k.(string))
nodeWalk(v, key, vars)
}
case string:
vars[key] = node.(string)
case int:
vars[key] = strconv.Itoa(node.(int))
case bool:
vars[key] = strconv.FormatBool(node.(bool))
case float64:
vars[key] = strconv.FormatFloat(node.(float64), 'f', -1, 64)
}
return nil
}
func (c *Client) watchChanges(watcher *fsnotify.Watcher, stopChan chan bool) ResultError {
outputChannel := make(chan ResultError)
go func() error {
defer close(outputChannel)
for {
select {
case event := <-watcher.Events:
log.Debug(fmt.Sprintf("Event: %s", event))
if event.Op&fsnotify.Write == fsnotify.Write ||
event.Op&fsnotify.Remove == fsnotify.Remove ||
event.Op&fsnotify.Create == fsnotify.Create {
outputChannel <- ResultError{response: 1, err: nil}
}
case err := <-watcher.Errors:
outputChannel <- ResultError{response: 0, err: err}
case <-stopChan:
outputChannel <- ResultError{response: 1, err: nil}
}
}
}()
return <-outputChannel
}
func (c *Client) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
if waitIndex == 0 {
return 1, nil
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return 0, err
}
defer watcher.Close()
for _, path := range c.filepath {
isDir, err := util.IsDirectory(path)
if err != nil {
return 0, err
}
if isDir {
dirs, err := util.RecursiveDirsLookup(path, "*")
if err != nil {
return 0, err
}
for _, dir := range dirs {
err = watcher.Add(dir)
if err != nil {
return 0, err
}
}
} else {
err = watcher.Add(path)
if err != nil {
return 0, err
}
}
}
output := c.watchChanges(watcher, stopChan)
if output.response != 2 {
return output.response, output.err
}
return waitIndex, nil
}