-
Notifications
You must be signed in to change notification settings - Fork 3
/
backends_env_test.go
81 lines (65 loc) · 1.71 KB
/
backends_env_test.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright confd. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE-confd file.
package libconfd
import (
"os"
"strings"
)
// tEnvClient provides a shell for the env client
type tEnvClient struct{}
func init() {
RegisterBackendClient(
(*tEnvClient)(nil).Type(),
func(cfg *BackendConfig) (BackendClient, error) {
p := tNewEnvClient()
return p, nil
},
)
}
// tNewEnvClient returns a new client
func tNewEnvClient() BackendClient {
return new(tEnvClient)
}
func (_ *tEnvClient) Type() string {
return "libconfd-backend-internal-env"
}
func (_ *tEnvClient) Close() error {
return nil
}
func (_ *tEnvClient) WatchEnabled() bool {
return false
}
// GetValues queries the environment for keys
func (_ *tEnvClient) GetValues(keys []string) (map[string]string, error) {
allEnvVars := os.Environ()
envMap := make(map[string]string)
for _, e := range allEnvVars {
index := strings.Index(e, "=")
envMap[e[:index]] = e[index+1:]
}
vars := make(map[string]string)
transform := func(key string) string {
var replacer = strings.NewReplacer("/", "_")
k := strings.TrimPrefix(key, "/")
return strings.ToUpper(replacer.Replace(k))
}
clean := func(key string) string {
var cleanReplacer = strings.NewReplacer("_", "/")
newKey := "/" + key
return cleanReplacer.Replace(strings.ToLower(newKey))
}
for _, key := range keys {
k := transform(key)
for envKey, envValue := range envMap {
if strings.HasPrefix(envKey, k) {
vars[clean(envKey)] = envValue
}
}
}
return vars, nil
}
func (_ *tEnvClient) WatchPrefix(prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error) {
<-stopChan
return 0, nil
}