-
-
Notifications
You must be signed in to change notification settings - Fork 481
/
funcs.go
98 lines (89 loc) · 2.02 KB
/
funcs.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
86
87
88
89
90
91
92
93
94
95
96
97
98
package tpl
import (
"context"
"crypto/md5"
"crypto/sha1"
"fmt"
"text/template"
"github.com/pkg/errors"
)
// These constants defined the template function names used
const (
FuncMd5sum = "md5sum"
FuncSha1sum = "sha1sum"
FuncGet = "get"
FuncGetPassword = "getpw"
FuncGetValue = "getval"
)
func md5sum() func(...string) (string, error) {
return func(s ...string) (string, error) {
return fmt.Sprintf("%x", md5.Sum([]byte(s[0]))), nil
}
}
func sha1sum() func(...string) (string, error) {
return func(s ...string) (string, error) {
return fmt.Sprintf("%x", sha1.Sum([]byte(s[0]))), nil
}
}
func get(ctx context.Context, kv kvstore) func(...string) (string, error) {
return func(s ...string) (string, error) {
if len(s) < 1 {
return "", nil
}
if kv == nil {
return "", errors.Errorf("KV is nil")
}
sec, err := kv.Get(ctx, s[0])
if err != nil {
return err.Error(), nil
}
buf, err := sec.Bytes()
if err != nil {
return err.Error(), nil
}
return string(buf), nil
}
}
func getPassword(ctx context.Context, kv kvstore) func(...string) (string, error) {
return func(s ...string) (string, error) {
if len(s) < 1 {
return "", nil
}
if kv == nil {
return "", errors.Errorf("KV is nil")
}
sec, err := kv.Get(ctx, s[0])
if err != nil {
return err.Error(), nil
}
return sec.Password(), nil
}
}
func getValue(ctx context.Context, kv kvstore) func(...string) (string, error) {
return func(s ...string) (string, error) {
if len(s) < 2 {
return "", nil
}
if kv == nil {
return "", errors.Errorf("KV is nil")
}
sec, err := kv.Get(ctx, s[0])
if err != nil {
return err.Error(), nil
}
val, err := sec.Value(s[1])
if err != nil {
return err.Error(), nil
}
return val, nil
}
}
func funcMap(ctx context.Context, kv kvstore) template.FuncMap {
return template.FuncMap{
FuncGet: get(ctx, kv),
FuncGetPassword: getPassword(ctx, kv),
FuncGetValue: getValue(ctx, kv),
FuncMd5sum: md5sum(),
FuncSha1sum: sha1sum(),
}
}