forked from gopasspw/gopass
/
grep.go
41 lines (33 loc) · 915 Bytes
/
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
package action
import (
"context"
"strings"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/utils/out"
"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)
}
search := c.Args().First()
l, err := s.Store.List(ctx, 0)
if err != nil {
return exitError(ctx, ExitList, err, "failed to list store: %s", err)
}
for _, v := range l {
sec, err := s.Store.Get(ctx, v)
if err != nil {
out.Red(ctx, "failed to decrypt %s: %v", v, err)
continue
}
if strings.Contains(sec.Password(), search) {
out.Print(ctx, "%s:\n%s", color.BlueString(v), sec.Password())
}
if strings.Contains(sec.Body(), search) {
out.Print(ctx, "%s:\n%s", color.BlueString(v), sec.Body())
}
}
return nil
}