-
Notifications
You must be signed in to change notification settings - Fork 9
/
base.go
85 lines (66 loc) · 1.88 KB
/
base.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
82
83
84
85
package secret
import (
"fmt"
"path"
"strings"
"github.com/FalcoSuessgott/vkv/pkg/utils"
"github.com/disiqueira/gotree/v3"
)
func (p *Printer) printBase(enginePath string, secrets map[string]interface{}) error {
var tree gotree.Tree
m := make(map[string]interface{})
for _, k := range utils.SortMapKeys(secrets) {
tree = gotree.New(enginePath + utils.Delimiter)
m = utils.ToMapStringInterface(secrets[k])
}
for _, i := range utils.SortMapKeys(m) {
//nolint: forcetypeassert
tree.AddTree(p.printTree(enginePath, i, m[i].(map[string]interface{})))
}
fmt.Fprint(p.writer, tree.Print())
return nil
}
func (p *Printer) printTree(rootPath, subPath string, m map[string]interface{}) gotree.Tree {
tree := gotree.New(p.buildTreeName(rootPath, subPath))
if strings.HasSuffix(subPath, utils.Delimiter) {
for _, i := range utils.SortMapKeys(m) {
//nolint: forcetypeassert
tree.AddTree(p.printTree(rootPath, subPath+i, m[i].(map[string]interface{})))
}
} else {
for _, k := range utils.SortMapKeys(m) {
if p.onlyKeys {
tree.Add(k)
}
if !p.onlyKeys && !p.onlyPaths {
tree.Add(fmt.Sprintf("%s=%v", k, m[k]))
}
}
}
return tree
}
func (p *Printer) buildTreeName(rootPath, subPath string) string {
name := subPath
subPathParts := strings.Split(strings.TrimSuffix(subPath, utils.Delimiter), utils.Delimiter)
if len(subPathParts) > 1 {
name = path.Base(subPath)
}
if p.showVersion {
if v, err := p.vaultClient.ReadSecretVersion(rootPath, subPath); err == nil {
name = fmt.Sprintf("v%v: %s", v, name)
}
}
if p.showMetadata {
if v, err := p.vaultClient.ReadSecretMetadata(rootPath, subPath); err == nil {
md := ""
metadata, ok := v.(map[string]interface{})
if ok {
for k, v := range metadata {
md = fmt.Sprintf("%v %s=%v", md, k, v)
}
name = fmt.Sprintf("%s [%v]", name, strings.TrimPrefix(md, " "))
}
}
}
return name
}