Skip to content

Commit

Permalink
Remove --clip option for find command
Browse files Browse the repository at this point in the history
When invoked as `gopass find`, there is no code-path where a value could
be copied to the clipboard, so the `--clip` option is always ignored.
  • Loading branch information
kpitt committed Sep 28, 2022
1 parent ccab0ac commit 743b4ff
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 52 deletions.
13 changes: 2 additions & 11 deletions docs/commands/find.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
# `find` command

The `find` command will attempt to do a simple substring match on the names of all secrets.
If there is a single match it will directly invoke `show` and display the result.
If there are multiple matches a selection will be shown.
The `find` command does a simple substring match on the names of all secrets,
and lists the matching secrets.

Note: The find command will not fall back to a fuzzy search.

## Synopsis

```
$ gopass find entry
$ gopass find -f entry
$ gopass find -c entry
```

## Flags

Flag | Aliases | Description
---- | ------- | -----------
`--clip` | `-c` | Copy the password into the clipboard.
15 changes: 3 additions & 12 deletions internal/action/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,13 @@ func (s *Action) GetCommands() []*cli.Command {
{
Name: "find",
Usage: "Search for secrets",
ArgsUsage: "[needle]",
ArgsUsage: "<pattern>",
Description: "" +
"This command will first attempt a simple pattern match on the name of the " +
"secret. If there is an exact match it will be shown directly; if there are " +
"multiple matches, a selection will be shown.",
"List all secrets that match the specified search pattern.",
Before: s.IsInitialized,
Action: s.FindNoFuzzy,
Action: s.Find,
Aliases: []string{"search"},
BashComplete: s.Complete,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "clip",
Aliases: []string{"c"},
Usage: "Copy the password into the clipboard",
},
},
},
{
Name: "fsck",
Expand Down
25 changes: 9 additions & 16 deletions internal/action/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,20 @@ import (
"github.com/urfave/cli/v2"
)

// FindNoFuzzy runs find without fuzzy search.
func (s *Action) FindNoFuzzy(c *cli.Context) error {
return s.findCmd(c, nil, false)
}

// Find runs find.
// Find runs the find command action, without fuzzy search.
func (s *Action) Find(c *cli.Context) error {
return s.findCmd(c, s.show, true)
}
if !c.Args().Present() {
return exit.Error(exit.Usage, nil, "Usage: %s find <pattern>", s.Name)
}

func (s *Action) findCmd(c *cli.Context, cb showFunc, fuzzy bool) error {
ctx := ctxutil.WithGlobalFlags(c)
if c.IsSet("clip") {
ctx = WithClip(ctx, c.Bool("clip"))
}

if !c.Args().Present() {
return exit.Error(exit.Usage, nil, "Usage: %s find <NEEDLE>", s.Name)
}
return s.find(ctx, c, c.Args().First(), nil, false)
}

return s.find(ctx, c, c.Args().First(), cb, fuzzy)
// FindFuzzy runs a fuzzy find of the specified name and attempts to show the result.
func (s *Action) FindFuzzy(c *cli.Context, name string) error {
return s.find(c.Context, c, name, s.show, true)
}

// see action.show - context, cli context, name, key, rescurse.
Expand Down
16 changes: 8 additions & 8 deletions internal/action/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ func TestFind(t *testing.T) { //nolint:paralleltest

// find
c := gptest.CliCtx(ctx, t)
if err := act.Find(c); err == nil || err.Error() != fmt.Sprintf("Usage: %s find <NEEDLE>", actName) {
if err := act.Find(c); err == nil || err.Error() != fmt.Sprintf("Usage: %s find <pattern>", actName) {
t.Errorf("Should fail: %s", err)
}

// find fo
// find fo (with fuzzy search)
c = gptest.CliCtxWithFlags(ctx, t, nil, "fo")
assert.NoError(t, act.Find(c))
assert.NoError(t, act.FindFuzzy(c, "fo"))
assert.Contains(t, strings.TrimSpace(buf.String()), "Found exact match in \"foo\"\nsecret")
buf.Reset()

// find fo (no fuzzy search)
c = gptest.CliCtxWithFlags(ctx, t, nil, "fo")
assert.NoError(t, act.FindNoFuzzy(c))
assert.NoError(t, act.Find(c))
assert.Equal(t, strings.TrimSpace(buf.String()), "foo")
buf.Reset()

// find yo
c = gptest.CliCtx(ctx, t, "yo")
assert.Error(t, act.Find(c))
c = gptest.CliCtx(ctx, t)
assert.Error(t, act.FindFuzzy(c, "yo"))
buf.Reset()

// add some secrets
Expand All @@ -76,8 +76,8 @@ func TestFind(t *testing.T) { //nolint:paralleltest
buf.Reset()

// find bar
c = gptest.CliCtx(ctx, t, "bar")
assert.NoError(t, act.Find(c))
c = gptest.CliCtx(ctx, t)
assert.NoError(t, act.FindFuzzy(c, "bar"))
assert.Equal(t, "bar/baz\nbar/zab", strings.TrimSpace(buf.String()))
buf.Reset()

Expand Down
9 changes: 4 additions & 5 deletions internal/action/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func showParseArgs(c *cli.Context) context.Context {
// Show the content of a secret file.
func (s *Action) Show(c *cli.Context) error {
name := c.Args().First()
if name == "" {
return exit.Error(exit.Usage, nil, "Usage: %s show [name]", s.Name)
}

ctx := showParseArgs(c)

Expand All @@ -81,10 +84,6 @@ func (s *Action) Show(c *cli.Context) error {

// show displays the given secret/key.
func (s *Action) show(ctx context.Context, c *cli.Context, name string, recurse bool) error {
if name == "" {
return exit.Error(exit.Usage, nil, "Usage: %s show [name]", s.Name)
}

if s.Store.IsDir(ctx, name) && !s.Store.Exists(ctx, name) {
return s.List(c)
}
Expand Down Expand Up @@ -272,7 +271,7 @@ func (s *Action) showHandleError(ctx context.Context, c *cli.Context, name strin

out.Warningf(ctx, "Entry %q not found. Starting search...", name)
c.Context = ctx
if err := s.Find(c); err != nil {
if err := s.FindFuzzy(c, name); err != nil {
return exit.Error(exit.NotFound, err, "%s", err)
}

Expand Down

0 comments on commit 743b4ff

Please sign in to comment.