Skip to content

Commit

Permalink
Do not fuzzy search no gopass find/search (#454)
Browse files Browse the repository at this point in the history
Fixes #453
  • Loading branch information
dominikschulz authored Nov 10, 2017
1 parent c337934 commit e460471
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
10 changes: 6 additions & 4 deletions action/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ func (s *Action) Find(ctx context.Context, c *cli.Context) error {
return s.show(ctx, c, choices[0], "", clip, false, false, false)
}

if len(choices) < 1 {
if len(choices) < 1 && ctxutil.IsFuzzySearch(ctx) {
// 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 len(choices) < 1 {
return fmt.Errorf("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 {
fmt.Println(value)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func main() {
"multiple matches a selection will be shown.",
Before: func(c *cli.Context) error { return action.Initialized(withGlobalFlags(ctx, c), c) },
Action: func(c *cli.Context) error {
return action.Find(withGlobalFlags(ctx, c), c)
return action.Find(withGlobalFlags(ctxutil.WithFuzzySearch(ctx, false), c), c)
},
Aliases: []string{"search"},
BashComplete: action.Complete,
Expand Down
15 changes: 15 additions & 0 deletions utils/ctxutil/ctxutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
ctxKeyAlwaysYes
ctxKeyUseSymbols
ctxKeyNoColor
ctxKeyFuzzySearch
)

// WithDebug returns a context with an explizit value for debug
Expand Down Expand Up @@ -302,3 +303,17 @@ func IsAlwaysYes(ctx context.Context) bool {
}
return bv
}

// WithFuzzySearch returns a context with the value for fuzzy search set
func WithFuzzySearch(ctx context.Context, fuzzy bool) context.Context {
return context.WithValue(ctx, ctxKeyFuzzySearch, fuzzy)
}

// IsFuzzySearch return the value of fuzzy search or the default (true)
func IsFuzzySearch(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyFuzzySearch).(bool)
if !ok {
return true
}
return bv
}

0 comments on commit e460471

Please sign in to comment.