/
secretlastpass.go
133 lines (119 loc) · 3.44 KB
/
secretlastpass.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
package cmd
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"sync"
"unicode"
"github.com/coreos/go-semver/semver"
"github.com/spf13/cobra"
)
var lastpassCmd = &cobra.Command{
Use: "lastpass [args...]",
Short: "Execute the LastPass CLI (lpass)",
PreRunE: config.ensureNoError,
RunE: config.runLastpassCmd,
}
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}
lastpassParseNoteRegexp = regexp.MustCompile(`\A([ A-Za-z]*):(.*)\z`)
lastpassVersionArgs = []string{"--version"}
lastpassVersionRegexp = regexp.MustCompile(`^LastPass CLI v(\d+\.\d+\.\d+)`)
)
type lastpassCmdConfig struct {
Command string
versionCheckOnce sync.Once
}
var lastPassCache = make(map[string][]map[string]interface{})
func init() {
config.Lastpass.Command = "lpass"
config.addTemplateFunc("lastpass", config.lastpassFunc)
config.addTemplateFunc("lastpassRaw", config.lastpassRawFunc)
secretCmd.AddCommand(lastpassCmd)
}
func (c *Config) runLastpassCmd(cmd *cobra.Command, args []string) error {
return c.run("", c.Lastpass.Command, args...)
}
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 := c.mutator.IdempotentCmdOutput(cmd)
if err != nil {
return nil, err
}
return output, nil
}
func (c *Config) lastpassRawFunc(id string) []map[string]interface{} {
c.Lastpass.versionCheckOnce.Do(func() {
panicOnError(c.lastpassVersionCheck())
})
if data, ok := lastPassCache[id]; ok {
return data
}
output, err := c.lastpassOutput("show", "--json", id)
panicOnError(err)
var data []map[string]interface{}
if err := json.Unmarshal(output, &data); err != nil {
panic(fmt.Errorf("parse error: %w\n%q", err, output))
}
lastPassCache[id] = data
return data
}
func (c *Config) lastpassFunc(id string) []map[string]interface{} {
data := c.lastpassRawFunc(id)
for _, d := range data {
if note, ok := d["note"].(string); ok {
d["note"] = lastpassParseNote(note)
}
}
return data
}
func (c *Config) lastpassVersionCheck() error {
output, err := c.lastpassOutput(lastpassVersionArgs...)
if err != nil {
return err
}
m := lastpassVersionRegexp.FindSubmatch(output)
if m == nil {
return fmt.Errorf("could not extract version from %q", output)
}
version, err := semver.NewVersion(string(m[1]))
if err != nil {
return err
}
if version.LessThan(lastpassMinVersion) {
return fmt.Errorf("version %s found, need version %s or later", version, lastpassMinVersion)
}
return nil
}
func lastpassParseNote(note string) map[string]string {
result := make(map[string]string)
s := bufio.NewScanner(bytes.NewBufferString(note))
key := ""
for s.Scan() {
if m := lastpassParseNoteRegexp.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 {
panic(fmt.Errorf("lastpassParseNote: %w", err))
}
return result
}