/
lastpasstemplatefuncs.go
144 lines (129 loc) · 3.27 KB
/
lastpasstemplatefuncs.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package cmd
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"unicode"
"github.com/coreos/go-semver/semver"
"github.com/twpayne/chezmoi/v2/pkg/chezmoilog"
)
var (
// chezmoi uses lpass show --json which was added in
// https://github.com/lastpass/lastpass-cli/commit/e5a22e2eeef31ab6c54595616e0f57ca0a1c162d
// and the first tag containing that commit is v1.3.0~6.
lastpassMinVersion = semver.Version{Major: 1, Minor: 3, Patch: 0}
lastpassParseNoteRx = regexp.MustCompile(`\A([ A-Za-z]*):(.*)\z`)
lastpassVersionArgs = []string{"--version"}
lastpassVersionRx = regexp.MustCompile(`^LastPass CLI v(\d+\.\d+\.\d+)`)
)
type lastpassConfig struct {
Command string
versionOK bool
cache map[string][]map[string]interface{}
}
func (c *Config) lastpassTemplateFunc(id string) []map[string]interface{} {
data, err := c.lastpassData(id)
if err != nil {
panic(err)
}
for _, d := range data {
if note, ok := d["note"].(string); ok {
d["note"], err = lastpassParseNote(note)
if err != nil {
panic(err)
}
}
}
return data
}
func (c *Config) lastpassRawTemplateFunc(id string) []map[string]interface{} {
data, err := c.lastpassData(id)
if err != nil {
panic(err)
}
return data
}
func (c *Config) lastpassData(id string) ([]map[string]interface{}, error) {
if !c.Lastpass.versionOK {
if err := c.lastpassVersionCheck(); err != nil {
return nil, err
}
c.Lastpass.versionOK = true
}
if data, ok := c.Lastpass.cache[id]; ok {
return data, nil
}
output, err := c.lastpassOutput("show", "--json", id)
if err != nil {
return nil, err
}
var data []map[string]interface{}
if err := json.Unmarshal(output, &data); err != nil {
return nil, fmt.Errorf("%s: parse error: %w", output, err)
}
if c.Lastpass.cache == nil {
c.Lastpass.cache = make(map[string][]map[string]interface{})
}
c.Lastpass.cache[id] = data
return data, nil
}
func (c *Config) lastpassOutput(args ...string) ([]byte, error) {
name := c.Lastpass.Command
cmd := exec.Command(name, args...)
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
output, err := chezmoilog.LogCmdOutput(cmd)
if err != nil {
return nil, err
}
return output, nil
}
func (c *Config) lastpassVersionCheck() error {
output, err := c.lastpassOutput("--version")
if err != nil {
return err
}
m := lastpassVersionRx.FindSubmatch(output)
if m == nil {
return &extractVersionError{
output: output,
}
}
version, err := semver.NewVersion(string(m[1]))
if err != nil {
return err
}
if version.LessThan(lastpassMinVersion) {
return &versionTooOldError{
have: version,
need: &lastpassMinVersion,
}
}
return nil
}
func lastpassParseNote(note string) (map[string]string, error) {
result := make(map[string]string)
s := bufio.NewScanner(bytes.NewBufferString(note))
key := ""
for s.Scan() {
if m := lastpassParseNoteRx.FindStringSubmatch(s.Text()); m != nil {
keyComponents := strings.Split(m[1], " ")
firstComponentRunes := []rune(keyComponents[0])
firstComponentRunes[0] = unicode.ToLower(firstComponentRunes[0])
keyComponents[0] = string(firstComponentRunes)
key = strings.Join(keyComponents, "")
result[key] = m[2] + "\n"
} else {
result[key] += s.Text() + "\n"
}
}
if err := s.Err(); err != nil {
return nil, err
}
return result, nil
}