forked from gopasspw/gopass
/
grep.go
53 lines (43 loc) · 1.23 KB
/
grep.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
package action
import (
"context"
"strings"
"github.com/gopasspw/gopass/pkg/out"
"github.com/fatih/color"
"github.com/urfave/cli"
)
// Grep searches a string inside the content of all files
func (s *Action) Grep(ctx context.Context, c *cli.Context) error {
if !c.Args().Present() {
return ExitError(ctx, ExitUsage, nil, "Usage: %s grep arg", s.Name)
}
// get the search term
needle := c.Args().First()
haystack, err := s.Store.List(ctx, 0)
if err != nil {
return ExitError(ctx, ExitList, err, "failed to list store: %s", err)
}
var matches int
var errors int
for _, v := range haystack {
sec, err := s.Store.Get(ctx, v)
if err != nil {
out.Error(ctx, "failed to decrypt %s: %v", v, err)
continue
}
if strings.Contains(sec.Password(), needle) {
out.Print(ctx, "%s:\n%s", color.BlueString(v), sec.Password())
}
if strings.Contains(sec.Body(), needle) {
out.Print(ctx, "%s:\n%s", color.BlueString(v), sec.Body())
}
}
if errors > 0 {
return ExitError(ctx, ExitDecrypt, nil, "some secrets failed to decrypt")
}
if matches < 1 {
return ExitError(ctx, ExitNotFound, nil, "no matches found")
}
out.Print(ctx, "\nScanned %d secrets. %d matches, %d errors", len(haystack), matches, errors)
return nil
}