forked from gliderlabs/connectable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsul.go
More file actions
57 lines (50 loc) · 1.12 KB
/
Copy pathconsul.go
File metadata and controls
57 lines (50 loc) · 1.12 KB
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
package main
import (
"log"
"net/url"
"github.com/armon/consul-api"
)
type ConsulStore struct {
client *consulapi.Client
waitIndex uint64
}
func NewConsulStore(uri *url.URL) ConfigStore {
config := consulapi.DefaultConfig()
if uri.Host != "" {
config.Address = uri.Host
}
client, err := consulapi.NewClient(config)
assert(err)
return &ConsulStore{client: client}
}
func (s *ConsulStore) List(path string) []string {
kv, _, err := s.client.KV().List(path, &consulapi.QueryOptions{})
if err != nil {
log.Println("consul:", err)
return []string{}
}
list := make([]string, 0)
for _, pair := range kv {
list = append(list, string(pair.Value))
}
return list
}
func (s *ConsulStore) Get(path string) string {
kv, _, err := s.client.KV().Get(path, &consulapi.QueryOptions{})
if err != nil {
log.Println("consul:", err)
return ""
}
if kv == nil {
return ""
}
return string(kv.Value)
}
func (s *ConsulStore) Watch(path string) {
_, meta, err := s.client.KV().Get(path, &consulapi.QueryOptions{WaitIndex: s.waitIndex})
if err != nil {
log.Println("consul:", err)
} else {
s.waitIndex = meta.LastIndex
}
}