forked from CiscoCloud/marathon-consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consul.go
53 lines (43 loc) · 956 Bytes
/
consul.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
package main
import (
"fmt"
"github.com/hashicorp/consul/api"
)
type Putter interface {
Put(*api.KVPair) (*api.WriteMeta, error)
}
type Deleter interface {
Delete(string) (*api.WriteMeta, error)
}
type PutDeleter interface {
Putter
Deleter
}
type KV struct {
kv *api.KV
WriteOptions *api.WriteOptions
Prefix string
}
func NewKV(config *api.Config) (*KV, error) {
client, err := api.NewClient(config)
if err != nil {
return nil, err
}
return &KV{
kv: client.KV(),
WriteOptions: &api.WriteOptions{},
}, nil
}
func (kv KV) ensurePrefix(key string) string {
if kv.Prefix != "" {
key = fmt.Sprintf("%s/%s", kv.Prefix, key)
}
return key
}
func (kv KV) Put(pair *api.KVPair) (*api.WriteMeta, error) {
pair.Key = kv.ensurePrefix(pair.Key)
return kv.kv.Put(pair, kv.WriteOptions)
}
func (kv KV) Delete(key string) (*api.WriteMeta, error) {
return kv.kv.Delete(kv.ensurePrefix(key), kv.WriteOptions)
}