-
Notifications
You must be signed in to change notification settings - Fork 4
/
kv.go
46 lines (40 loc) · 921 Bytes
/
kv.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
package registry
import (
"github.com/hashicorp/consul/api"
)
type kv struct {
client *api.KV
}
func (kv *kv) Get(key string) (string, error) {
v, _, err := kv.client.Get(RegistryPrefix+"/"+key, nil)
if err != nil {
return "", err
}
if v != nil {
return string(v.Value), nil
}
return "", nil
}
func (kv *kv) Set(key, value string) error {
if _, err := kv.client.Put(&api.KVPair{Key: RegistryPrefix + "/" + key, Value: []byte(value)}, nil); err != nil {
return err
}
return nil
}
func (kv *kv) List(prefix string) (map[string]string, error) {
items, _, err := kv.client.List(prefix, nil)
if err != nil {
return nil, err
}
list := make(map[string]string, len(items))
for _, k := range items {
list[k.Key] = string(k.Value)
}
return list, nil
}
func (kv *kv) Delete(key string) error {
if _, err := kv.client.Delete(RegistryPrefix+"/"+key, nil); err != nil {
return err
}
return nil
}