lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 19669f3a5ad49223a058ff32cd40ef956505f35a
parent 2136977ae685beb3504930c4f1a97e8837ad56fd
Author: Sean Enck <sean@ttypty.com>
Date:   Wed,  4 Jun 2025 19:28:04 -0400

add 'find' command back in

Diffstat:
Mcmd/lb/main.go | 4++--
Mcmd/lb/main_test.go | 1+
Mcmd/lb/tests/expected.log | 1+
Minternal/app/completions/core.go | 2+-
Minternal/app/list.go | 30++++++++++++++++++++++++++----
Minternal/app/list_test.go | 47+++++++++++++++++++++++++++++++++++++++++++++--
6 files changed, 76 insertions(+), 9 deletions(-)

diff --git a/cmd/lb/main.go b/cmd/lb/main.go @@ -71,8 +71,8 @@ func run() error { switch command { case commands.ReKey: return app.ReKey(p) - case commands.List: - return app.List(p) + case commands.List, commands.Find: + return app.List(p, command == commands.Find) case commands.Move: return app.Move(p) case commands.Insert, commands.MultiLine: diff --git a/cmd/lb/main_test.go b/cmd/lb/main_test.go @@ -252,6 +252,7 @@ func test(profile string) error { r.run("", "mv move/ma/ka3/* move/mac/") r.run("", "mv key/a/one keyx/d/e") r.run("", "ls") + r.run("", "find keyx") r.run("echo y |", "rm move/*") r.run("echo y |", "rm keyx/d/e") r.logAppend("echo") diff --git a/cmd/lb/tests/expected.log b/cmd/lb/tests/expected.log @@ -75,6 +75,7 @@ move/ma/ka3/yyy move/ma/ka3/zzz move/mac/yyy move/mac/zzz +keyx/d/e selected entities: move/m/ka/abc move/m/ka/xyz diff --git a/internal/app/completions/core.go b/internal/app/completions/core.go @@ -119,7 +119,7 @@ func Generate(completionType, exe string) ([]string, error) { } c.Conditionals = NewConditionals() - c.Options = c.newGenOptions([]string{commands.Help, commands.List, commands.Show, commands.Version, commands.JSON}, + c.Options = c.newGenOptions([]string{commands.Help, commands.List, commands.Show, commands.Version, commands.JSON, commands.Find}, map[string]string{ commands.Clip: c.Conditionals.Not.CanClip, commands.TOTP: c.Conditionals.Not.CanTOTP, diff --git a/internal/app/list.go b/internal/app/list.go @@ -4,28 +4,50 @@ package app import ( "errors" "fmt" + "regexp" "git.sr.ht/~enckse/lockbox/internal/backend" ) // List will list/find entries -func List(cmd CommandOptions) error { +func List(cmd CommandOptions, isFind bool) error { args := cmd.Args() opts := backend.QueryOptions{} opts.Mode = backend.ListMode - if len(args) != 0 { - return errors.New("list does not support any arguments") + if isFind { + if len(args) != 1 { + return errors.New("find requires one argument") + } + } else { + if len(args) != 0 { + return errors.New("list does not support any arguments") + } } e, err := cmd.Transaction().QueryCallback(opts) if err != nil { return err } w := cmd.Writer() + printer := func(p string) { + fmt.Fprintf(w, "%s\n", p) + } + finder := printer + if isFind { + re, err := regexp.Compile(args[0]) + if err != nil { + return err + } + finder = func(p string) { + if re.MatchString(p) { + printer(p) + } + } + } for f, err := range e { if err != nil { return err } - fmt.Fprintf(w, "%s\n", f.Path) + finder(f.Path) } return nil } diff --git a/internal/app/list_test.go b/internal/app/list_test.go @@ -3,6 +3,7 @@ package app_test import ( "os" "path/filepath" + "strings" "testing" "git.sr.ht/~enckse/lockbox/internal/app" @@ -42,14 +43,56 @@ func setup(t *testing.T) *backend.Transaction { func TestList(t *testing.T) { m := newMockCommand(t) - if err := app.List(m); err != nil { + if err := app.List(m, false); err != nil { t.Errorf("invalid error: %v", err) } if m.buf.String() == "" { t.Error("nothing listed") } m.args = []string{"test"} - if err := app.List(m); err.Error() != "list does not support any arguments" { + if err := app.List(m, false); err == nil || err.Error() != "list does not support any arguments" { t.Errorf("invalid error: %v", err) } } + +func TestFind(t *testing.T) { + m := newMockCommand(t) + if err := app.List(m, true); err == nil || err.Error() != "find requires one argument" { + t.Errorf("invalid error: %v", err) + } + if m.buf.String() != "" { + t.Error("something listed") + } + m.buf.Reset() + m.args = []string{"["} + if err := app.List(m, true); err == nil || !strings.Contains(err.Error(), "missing closing") { + t.Errorf("invalid error: %v", err) + } + if m.buf.String() != "" { + t.Error("something listed") + } + m.buf.Reset() + m.args = []string{"test", "1"} + if err := app.List(m, true); err == nil || err.Error() != "find requires one argument" { + t.Errorf("invalid error: %v", err) + } + if m.buf.String() != "" { + t.Error("something listed") + } + m.buf.Reset() + m.args = []string{"[zzzzzz]"} + if err := app.List(m, true); err != nil { + t.Errorf("invalid error: %v", err) + } + if m.buf.String() != "" { + t.Error("something listed") + } + m.buf.Reset() + m.args = []string{"test"} + if err := app.List(m, true); err != nil { + t.Errorf("invalid error: %v", err) + } + if m.buf.String() == "" { + t.Error("nothing listed") + } +}