forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
76 lines (66 loc) · 1.87 KB
/
find.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
package action
import (
"context"
"fmt"
"strings"
"github.com/justwatchcom/gopass/utils/ctxutil"
"github.com/justwatchcom/gopass/utils/out"
"github.com/justwatchcom/gopass/utils/termwiz"
"github.com/schollz/closestmatch"
"github.com/urfave/cli"
)
// Find a string in the secret file's name
func (s *Action) Find(ctx context.Context, c *cli.Context) error {
if !c.Args().Present() {
return s.exitError(ctx, ExitUsage, nil, "Usage: %s find arg", s.Name)
}
l, err := s.Store.List(0)
if err != nil {
return s.exitError(ctx, ExitList, err, "failed to list store: %s", err)
}
needle := strings.ToLower(c.Args().First())
choices := make([]string, 0, 10)
for _, value := range l {
if strings.Contains(strings.ToLower(value), needle) {
choices = append(choices, value)
}
}
clip := c.Bool("clip")
if len(choices) == 1 {
out.Green(ctx, "Found exact match in '%s'", choices[0])
return s.show(ctx, c, choices[0], "", clip, false, false, false)
}
if len(choices) < 1 {
// try fuzzy match
cm := closestmatch.New(l, []int{2})
choices = cm.ClosestN(needle, 5)
if len(choices) < 1 {
return fmt.Errorf("no results found")
}
}
if !ctxutil.IsTerminal(ctx) || c.Command.Name == "find" {
for _, value := range choices {
fmt.Println(value)
}
return nil
}
act, sel := termwiz.GetSelection(ctx, "Found secrets -", "", choices)
switch act {
case "copy":
// display selected entry
fmt.Println(choices[sel])
return s.show(ctx, c, choices[sel], "", true, false, false, false)
case "show":
// display selected entry
fmt.Println(choices[sel])
return s.show(ctx, c, choices[sel], "", clip, false, false, false)
case "sync":
// run sync and re-run show/find workflow
if err := s.Sync(ctx, c); err != nil {
return err
}
return s.show(ctx, c, needle, "", clip, false, false, true)
default:
return s.exitError(ctx, ExitAborted, nil, "user aborted")
}
}