-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
repl.go
198 lines (172 loc) · 4.59 KB
/
repl.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package action
import (
"context"
"fmt"
"strings"
"github.com/ergochat/readline"
"github.com/gopasspw/gopass/internal/out"
"github.com/gopasspw/gopass/internal/tree"
"github.com/gopasspw/gopass/pkg/debug"
shellquote "github.com/kballard/go-shellquote"
"github.com/urfave/cli/v2"
)
func (s *Action) entriesForCompleter(ctx context.Context) ([]readline.PrefixCompleterInterface, error) {
args := []readline.PrefixCompleterInterface{}
list, err := s.Store.List(ctx, tree.INF)
if err != nil {
return args, err
}
for _, v := range list {
args = append(args, readline.PcItem(v))
}
return args, nil
}
func (s *Action) replCompleteRecipients(ctx context.Context, cmd *cli.Command) []readline.PrefixCompleterInterface {
subCmds := []readline.PrefixCompleterInterface{}
if cmd.Name == "remove" {
for _, r := range s.recipientsList(ctx) {
subCmds = append(subCmds, readline.PcItem(r))
}
}
args := []readline.PrefixCompleterInterface{}
args = append(args, readline.PcItem(cmd.Name, subCmds...))
for _, alias := range cmd.Aliases {
args = append(args, readline.PcItem(alias, subCmds...))
}
return args
}
func (s *Action) replCompleteTemplates(ctx context.Context, cmd *cli.Command) []readline.PrefixCompleterInterface {
subCmds := []readline.PrefixCompleterInterface{}
for _, r := range s.templatesList(ctx) {
subCmds = append(subCmds, readline.PcItem(r))
}
args := []readline.PrefixCompleterInterface{}
args = append(args, readline.PcItem(cmd.Name, subCmds...))
for _, alias := range cmd.Aliases {
args = append(args, readline.PcItem(alias, subCmds...))
}
return args
}
func (s *Action) prefixCompleter(c *cli.Context) *readline.PrefixCompleter {
secrets, err := s.entriesForCompleter(c.Context)
if err != nil {
debug.Log("failed to list secrets: %s", err)
}
cmds := []readline.PrefixCompleterInterface{}
for _, cmd := range c.App.Commands {
if cmd.Hidden {
continue
}
subCmds := []readline.PrefixCompleterInterface{}
switch cmd.Name {
case "config":
for _, k := range s.configKeys() {
subCmds = append(subCmds, readline.PcItem(k))
}
case "recipients":
subCmds = append(subCmds, s.replCompleteRecipients(c.Context, cmd)...)
case "templates":
subCmds = append(subCmds, s.replCompleteTemplates(c.Context, cmd)...)
case "cat":
fallthrough
case "delete":
fallthrough
case "edit":
fallthrough
case "generate":
fallthrough
case "history":
fallthrough
case "list":
fallthrough
case "move":
fallthrough
case "otp":
fallthrough
case "show":
subCmds = append(subCmds, secrets...)
default:
}
for _, scmd := range cmd.Subcommands {
subCmds = append(subCmds, readline.PcItem(scmd.Name))
}
cmds = append(cmds, readline.PcItem(cmd.Name, subCmds...))
for _, alias := range cmd.Aliases {
cmds = append(cmds, readline.PcItem(alias, subCmds...))
}
}
return readline.NewPrefixCompleter(cmds...)
}
// REPL implements a read-execute-print-line shell
// with readline support and autocompletion.
func (s *Action) REPL(c *cli.Context) error {
c.App.ExitErrHandler = func(c *cli.Context, err error) {
if err == nil {
return
}
out.Errorf(c.Context, "%s", err)
}
out.Printf(c.Context, logo)
out.Printf(c.Context, "🌟 Welcome to gopass!")
out.Printf(c.Context, "⚠ This is the built-in shell. Type 'help' for a list of commands.")
rl, err := readline.New("gopass> ")
if err != nil {
return err
}
defer func() {
_ = rl.Close()
}()
READ:
for {
// check for context cancellation
select {
case <-c.Context.Done():
return fmt.Errorf("user aborted")
default:
}
// we need to update the completer on every loop since
// the list of secrets may have changed, e.g. due to
// the user adding a new secret.
cfg := rl.GetConfig()
cfg.AutoComplete = s.prefixCompleter(c)
if err := rl.SetConfig(cfg); err != nil {
debug.Log("Failed to set readline config: %s", err)
break
}
line, err := rl.Readline()
if err != nil {
debug.Log("Readline error: %s", err)
break
}
args, err := shellquote.Split(line)
if err != nil {
out.Printf(c.Context, "Error: %s", err)
continue
}
if len(args) < 1 {
continue
}
switch strings.ToLower(args[0]) {
case "quit":
break READ
case "lock":
s.replLock(c.Context)
continue
case "clear":
rl.ClearScreen()
continue
default:
}
if err := c.App.RunContext(c.Context, append([]string{"gopass"}, args...)); err != nil {
continue
}
}
return nil
}
func (s *Action) replLock(ctx context.Context) {
if err := s.Store.Lock(); err != nil {
out.Errorf(ctx, "Failed to lock stores: %s", err)
return
}
out.OKf(ctx, "Locked")
}