-
Notifications
You must be signed in to change notification settings - Fork 3
/
etcd.go
34 lines (31 loc) · 895 Bytes
/
etcd.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
package main
import (
"github.com/coreos/go-etcd/etcd"
"strings"
)
// Retrieves all unique values for a given key from a node and its children.
func getEtcdValues(node *etcd.Node, key string) []string {
valuesMap := map[string]bool{} // (track vaules in a map, for uniqueness)
if node.Value != "" {
if lastKey(node.Key) == key {
valuesMap[node.Value] = true
}
} else {
for _, subNode := range node.Nodes {
subValues := getEtcdValues(subNode, key)
for _, subValue := range subValues {
valuesMap[subValue] = true
}
}
}
values := []string{} // compute just the keys of the valuesMap
for value, _ := range valuesMap {
values = append(values, value)
}
return values
}
// Returns the last component of an etcd key
func lastKey(key string) string {
components := strings.Split(key, "/")
return strings.Join(components[len(components)-1:len(components)], "/")
}