-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.go
45 lines (35 loc) · 949 Bytes
/
tree.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
package vault
import (
"path/filepath"
)
//go:generate counterfeiter -fake-name Repository -o ./fake/repository.go . Repository
// Repository is a repository of the secrets
type Repository interface {
// Secret provides the secret from the backend
Secret(path string) (map[string]interface{}, error)
// Stop stops the repository
Stop()
}
var _ Repository = &RepositoryTree{}
// RepositoryTree caches the secrets
type RepositoryTree struct {
Repository Repository
Root map[string]map[string]interface{}
}
// Secret returns value from a tree
func (r *RepositoryTree) Secret(path string) (map[string]interface{}, error) {
path = filepath.Join(splitBy(path, "/")...)
node, found := r.Root[path]
if !found {
var err error
if node, err = r.Repository.Secret(path); err != nil {
return nil, err
}
}
r.Root[path] = node
return node, nil
}
// Stop stops the tree
func (r *RepositoryTree) Stop() {
r.Repository.Stop()
}