forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find.go
102 lines (88 loc) · 2.91 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
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
package action
import (
"context"
"fmt"
"sort"
"strings"
"github.com/justwatchcom/gopass/pkg/ctxutil"
"github.com/justwatchcom/gopass/pkg/cui"
"github.com/justwatchcom/gopass/pkg/out"
"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 {
ctx = WithClip(ctx, c.Bool("clip"))
if !c.Args().Present() {
return ExitError(ctx, ExitUsage, nil, "Usage: %s find <NEEDLE>", s.Name)
}
// get all existing entries
haystack, err := s.Store.List(ctx, 0)
if err != nil {
return ExitError(ctx, ExitList, err, "failed to list store: %s", err)
}
// filter our the ones from the haystack matching the needle
needle := strings.ToLower(c.Args().First())
choices := filter(haystack, needle)
// if we have an exact match print it
if len(choices) == 1 {
out.Green(ctx, "Found exact match in '%s'", choices[0])
return s.show(ctx, c, choices[0], "", false)
}
// if we don't have a match yet try a fuzzy search
if len(choices) < 1 && ctxutil.IsFuzzySearch(ctx) {
// try fuzzy match
cm := closestmatch.New(haystack, []int{2})
choices = cm.ClosestN(needle, 5)
}
// if there are still no results we abort
if len(choices) < 1 {
return ExitError(ctx, ExitNotFound, nil, "no results found")
}
// do not invoke wizard if not printing to terminal or if
// gopass find/search was invoked directly (for scripts)
if !ctxutil.IsTerminal(ctx) || c.Command.Name == "find" {
for _, value := range choices {
out.Print(ctx, value)
}
return nil
}
return s.findSelection(ctx, c, choices, needle)
}
// findSelection runs a wizard that lets the user select an entry
func (s *Action) findSelection(ctx context.Context, c *cli.Context, choices []string, needle string) error {
sort.Strings(choices)
act, sel := cui.GetSelection(ctx, "Found secrets - Please select an entry", "<↑/↓> to change the selection, <→> to show, <←> to copy, <s> to sync, <ESC> to quit", choices)
out.Debug(ctx, "Action: %s - Selection: %d", act, sel)
switch act {
case "default":
// display or copy selected entry
fmt.Fprintln(stdout, choices[sel])
return s.show(ctx, c, choices[sel], "", false)
case "copy":
// display selected entry
fmt.Fprintln(stdout, choices[sel])
return s.show(WithClip(ctx, true), c, choices[sel], "", false)
case "show":
// display selected entry
fmt.Fprintln(stdout, choices[sel])
return s.show(WithClip(ctx, false), c, choices[sel], "", 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, "", true)
default:
return ExitError(ctx, ExitAborted, nil, "user aborted")
}
}
func filter(l []string, needle string) []string {
choices := make([]string, 0, 10)
for _, value := range l {
if strings.Contains(strings.ToLower(value), needle) {
choices = append(choices, value)
}
}
return choices
}