forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
46 lines (38 loc) · 983 Bytes
/
registry.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 (
"encoding/json"
"errors"
"fmt"
"github.com/coreos/fleet/third_party/github.com/coreos/go-etcd/etcd"
)
const DefaultKeyPrefix = "/_coreos.com/fleet/"
type Registry struct {
etcd *etcd.Client
keyPrefix string
}
func New(client *etcd.Client, keyPrefix string) (registry *Registry) {
return &Registry{client, keyPrefix}
}
func (r *Registry) GetDebugInfo() (string, error) {
resp, err := r.etcd.RawGet(r.keyPrefix, true, true)
if err != nil {
return "", err
}
return string(resp.Body), nil
}
func marshal(obj interface{}) (string, error) {
encoded, err := json.Marshal(obj)
if err == nil {
return string(encoded), nil
} else {
return "", errors.New(fmt.Sprintf("Unable to JSON-serialize object: %s", err))
}
}
func unmarshal(val string, obj interface{}) error {
err := json.Unmarshal([]byte(val), &obj)
if err == nil {
return nil
} else {
return errors.New(fmt.Sprintf("Unable to JSON-deserialize object: %s", err))
}
}