-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
yaml.go
86 lines (75 loc) · 1.69 KB
/
yaml.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
package secret
import (
"fmt"
"strings"
"github.com/gopasspw/gopass/pkg/store"
yaml "gopkg.in/yaml.v2"
)
// Value returns the value of the given key if the body contained valid
// YAML
func (s *Secret) Value(key string) (string, error) {
s.Lock()
defer s.Unlock()
if s.data == nil {
if !strings.HasPrefix(s.body, "---\n") {
return "", store.ErrYAMLNoMark
}
if err := s.decode(); err != nil {
return "", err
}
}
if v, found := s.data[key]; found {
return fmt.Sprintf("%v", v), nil
}
return "", store.ErrYAMLNoKey
}
// SetValue sets a key to a given value. Will fail if an non-empty body exists
func (s *Secret) SetValue(key, value string) error {
s.Lock()
defer s.Unlock()
if s.body == "" && s.data == nil {
s.data = make(map[string]interface{}, 1)
}
if s.data == nil {
return store.ErrYAMLNoMark
}
s.data[key] = value
return s.encode()
}
// DeleteKey key will delete a single key from an decoded map
func (s *Secret) DeleteKey(key string) error {
s.Lock()
defer s.Unlock()
if s.data == nil {
return store.ErrYAMLNoMark
}
delete(s.data, key)
return s.encode()
}
// decodeYAML attempts to decode an optional YAML part of a secret
func (s *Secret) decodeYAML() (bool, error) {
if !strings.HasPrefix(s.body, "---\n") && s.password != "---" {
return false, nil
}
d := make(map[string]interface{})
err := yaml.Unmarshal([]byte(s.body), &d)
if err != nil {
return true, err
}
s.data = d
return true, nil
}
func (s *Secret) encodeYAML() (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic: %s", r)
}
}()
// update body
yb, err := yaml.Marshal(s.data)
if err != nil {
return err
}
s.body = "---\n" + string(yb)
return err
}