summaryrefslogtreecommitdiff
path: root/libkv/libkv.go
blob: 881bfbf00bd5f6d79186587ea90e07e75815784f (plain)
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
58
59
60
61
62
63
64
package libkv

import (
	"bytes"
	"encoding/json"
	"strings"

	"github.com/docker/libkv/store"
)

// LibKV -
type LibKV struct {
	store store.Store
}

// Login -
func (kv *LibKV) Login() error {
	return nil
}

// Logout -
func (kv *LibKV) Logout() {
}

// Read -
func (kv *LibKV) Read(path string) ([]byte, error) {
	data, err := kv.store.Get(path)
	if err != nil {
		return nil, err
	}

	return data.Value, nil
}

// List -
func (kv *LibKV) List(path string) ([]byte, error) {
	data, err := kv.store.List(path)
	if err != nil {
		return nil, err
	}

	result := []map[string]string{}
	for _, pair := range data {
		// Remove the path from the key
		key := strings.TrimPrefix(
			pair.Key,
			strings.TrimLeft(path, "/"),
		)
		result = append(
			result,
			map[string]string{
				"key":   key,
				"value": string(pair.Value),
			},
		)
	}

	var buf bytes.Buffer
	enc := json.NewEncoder(&buf)
	if err := enc.Encode(result); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}