lockbox

password manager
Log | Files | Refs | README | LICENSE

commit a3cdea44afba0d945053322c4d48a77af2c767a5
parent d33857c053a5fc13a60cb0127d64ead2a281834d
Author: Sean Enck <sean@ttypty.com>
Date:   Thu,  2 Mar 2023 19:32:58 -0500

moved out list/find

Diffstat:
Minternal/app/core.go | 24++++--------------------
Ainternal/commands/listfind.go | 35+++++++++++++++++++++++++++++++++++
Ainternal/commands/listfind_test.go | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 107 insertions(+), 20 deletions(-)

diff --git a/internal/app/core.go b/internal/app/core.go @@ -55,7 +55,8 @@ func Run() error { return errors.New("requires subcommand") } command := args[1] - ok, err := handleEarly(command, args[2:]) + sub := args[2:] + ok, err := handleEarly(command, sub) if err != nil { return err } @@ -69,27 +70,10 @@ func Run() error { switch command { case cli.ReKeyCommand: if confirm("proceed with rekey") { - if err := t.ReKey(); err != nil { - return wrapped("unable to rekey", err) - } + return t.ReKey() } case cli.ListCommand, cli.FindCommand: - opts := backend.QueryOptions{} - opts.Mode = backend.ListMode - if command == cli.FindCommand { - opts.Mode = backend.FindMode - if len(args) < 3 { - return errors.New("find requires search term") - } - opts.Criteria = args[2] - } - e, err := t.QueryCallback(opts) - if err != nil { - return wrapped("unable to list files", err) - } - for _, f := range e { - fmt.Println(f.Path) - } + return commands.ListFind(t, os.Stdout, command, sub) case cli.MoveCommand: if len(args) != 4 { return errors.New("src/dst required for move") diff --git a/internal/commands/listfind.go b/internal/commands/listfind.go @@ -0,0 +1,35 @@ +package commands + +import ( + "errors" + "fmt" + "io" + + "github.com/enckse/lockbox/internal/backend" + "github.com/enckse/lockbox/internal/cli" +) + +// ListFind will list/find entries +func ListFind(t *backend.Transaction, w io.Writer, command string, args []string) error { + opts := backend.QueryOptions{} + opts.Mode = backend.ListMode + if command == cli.FindCommand { + opts.Mode = backend.FindMode + if len(args) < 1 { + return errors.New("find requires search term") + } + opts.Criteria = args[0] + } else { + if len(args) != 0 { + return errors.New("list does not support any arguments") + } + } + e, err := t.QueryCallback(opts) + if err != nil { + return err + } + for _, f := range e { + fmt.Fprintf(w, "%s\n", f.Path) + } + return nil +} diff --git a/internal/commands/listfind_test.go b/internal/commands/listfind_test.go @@ -0,0 +1,68 @@ +package commands_test + +import ( + "bytes" + "os" + "strings" + "testing" + + "github.com/enckse/lockbox/internal/backend" + "github.com/enckse/lockbox/internal/commands" +) + +func fullSetup(t *testing.T, keep bool) *backend.Transaction { + if !keep { + os.Remove("test.kdbx") + } + os.Setenv("LOCKBOX_READONLY", "no") + os.Setenv("LOCKBOX_STORE", "test.kdbx") + os.Setenv("LOCKBOX_KEY", "test") + os.Setenv("LOCKBOX_KEYFILE", "") + os.Setenv("LOCKBOX_KEYMODE", "plaintext") + os.Setenv("LOCKBOX_TOTP", "totp") + os.Setenv("LOCKBOX_HOOKDIR", "") + os.Setenv("LOCKBOX_SET_MODTIME", "") + tr, err := backend.NewTransaction() + if err != nil { + t.Errorf("failed: %v", err) + } + return tr +} + +func setup(t *testing.T) *backend.Transaction { + return fullSetup(t, false) +} + +func TestList(t *testing.T) { + setup(t) + fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test1"), "pass") + fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test3"), "pass") + tx := fullSetup(t, true) + var buf bytes.Buffer + if err := commands.ListFind(tx, &buf, "list", []string{}); err != nil { + t.Errorf("invalid error: %v", err) + } + if buf.String() == "" { + t.Error("nothing listed") + } + if err := commands.ListFind(tx, &buf, "list", []string{"test"}); err.Error() != "list does not support any arguments" { + t.Errorf("invalid error: %v", err) + } +} + +func TestFind(t *testing.T) { + setup(t) + fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test1"), "pass") + fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test3"), "pass") + tx := fullSetup(t, true) + var buf bytes.Buffer + if err := commands.ListFind(tx, &buf, "find", []string{}); err.Error() != "find requires search term" { + t.Errorf("invalid error: %v", err) + } + if err := commands.ListFind(tx, &buf, "find", []string{"test1"}); err != nil { + t.Errorf("invalid error: %v", err) + } + if buf.String() == "" || strings.Contains(buf.String(), "test3") { + t.Error("wrong find") + } +}