-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
60 lines (48 loc) · 1.33 KB
/
config.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
54
55
56
57
58
59
60
package lochness
import (
"errors"
"path/filepath"
"strings"
"github.com/mistifyio/lochness/pkg/kv"
)
//Used to get set arbitrary config variables
var (
// ConfigPath is the path in the config store.
ConfigPath = "lochness/config/"
)
// GetConfig gets a single value from the config store. The key can contain slashes ("/")
func (c *Context) GetConfig(key string) (string, error) {
if key == "" {
return "", errors.New("empty config key")
}
resp, err := c.kv.Get(filepath.Join(ConfigPath, key))
if err != nil {
return "", err
}
return string(resp.Data), nil
}
// SetConfig sets a single value from the config store. The key can contain slashes ("/")
func (c *Context) SetConfig(key, val string) error {
if key == "" {
return errors.New("empty config key")
}
err := c.kv.Set(filepath.Join(ConfigPath, key), val)
return err
}
// ForEachConfig will run f on each config. It will stop iteration if f returns an error.
func (c *Context) ForEachConfig(f func(key, val string) error) error {
nodes, err := c.kv.GetAll(ConfigPath)
if err != nil {
return err
}
return forEachConfig(nodes, f)
}
func forEachConfig(nodes map[string]kv.Value, f func(key, val string) error) error {
for k, v := range nodes {
k = strings.TrimPrefix(k, ConfigPath)
if err := f(k, string(v.Data)); err != nil {
return err
}
}
return nil
}