-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconfig.go
More file actions
87 lines (74 loc) · 2.34 KB
/
config.go
File metadata and controls
87 lines (74 loc) · 2.34 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package mock
import (
"fmt"
"regexp"
"github.com/deis/deis/deisctl/config/model"
)
// Store for a mocked config backend
type Store []*model.ConfigNode
// ConfigBackend is an in memory "mock" config datastore used for testing
type ConfigBackend struct {
Expected Store
}
// Get a value by key from an in memory config backend
func (cb ConfigBackend) Get(key string) (value string, err error) {
for _, expect := range cb.Expected {
if expect.Key == key {
return expect.Value, nil
}
}
return "", fmt.Errorf("%s does not exist", cb.Expected)
}
// GetWithDefault gets a value by key from an in memory config backend and
// return a default value if not found
func (cb ConfigBackend) GetWithDefault(key string, defaultValue string) (string, error) {
for _, expect := range cb.Expected {
if expect.Key == key {
return expect.Value, nil
}
}
return defaultValue, nil
}
// Set a value for the specified key in an in memory config backend
func (cb ConfigBackend) Set(key, value string) (returnedValue string, err error) {
for _, expect := range cb.Expected {
if expect.Key == key {
return value, nil
}
}
return "", fmt.Errorf("%s does not exist", cb.Expected)
}
// Delete a key/value pair by key from an in memory config backend
func (cb ConfigBackend) Delete(key string) (err error) {
for _, expect := range cb.Expected {
if expect.Key == key {
return nil
}
}
return fmt.Errorf("%s does not exist", cb.Expected)
}
// GetRecursive returns a slice of all key/value pairs "under" a specified key
// in an in memory config backend (this is assuming some hierarchichal
// order exists wherein the value corresponding to a key may in fact be another
// key/value pair)
func (cb ConfigBackend) GetRecursive(key string) ([]*model.ConfigNode, error) {
r, _ := regexp.Compile(`^deis/services\s+`)
var configNodes []*model.ConfigNode
for _, expect := range cb.Expected {
if r.MatchString(expect.Key) {
configNodes = append(configNodes, expect)
}
}
return configNodes, nil
}
// SetWithTTL sets a value for the specified key in an in memory config
// backend-- with a time to live
func (cb ConfigBackend) SetWithTTL(key string, value string, ttl uint64) (string, error) {
for _, expect := range cb.Expected {
if expect.Key == key {
expect.TTL = int64(ttl)
return expect.Key, nil
}
}
return "", fmt.Errorf("%s does not exist", cb.Expected)
}