lockbox

password manager
Log | Files | Refs | README | LICENSE

commit b45a1dda0a422f538a4fb61d73325a4151e0d30a
parent d6ad4c1949694dfbe2520d2ed3c8d0b0091d477a
Author: Sean Enck <sean@ttypty.com>
Date:   Thu,  2 Mar 2023 20:16:50 -0500

show/clip moved

Diffstat:
Minternal/app/core.go | 27+--------------------------
Ainternal/commands/showclip.go | 42++++++++++++++++++++++++++++++++++++++++++
Ainternal/commands/showclip_test.go | 38++++++++++++++++++++++++++++++++++++++
3 files changed, 81 insertions(+), 26 deletions(-)

diff --git a/internal/app/core.go b/internal/app/core.go @@ -141,32 +141,7 @@ func Run() error { case cli.StatsCommand: return commands.Stats(os.Stdout, t, sub) case cli.ShowCommand, cli.ClipCommand: - if len(args) != 3 { - return errors.New("entry required") - } - entry := args[2] - clipboard := platform.Clipboard{} - isShow := command == cli.ShowCommand - if !isShow { - clipboard, err = platform.NewClipboard() - if err != nil { - return wrapped("unable to get clipboard", err) - } - } - existing, err := t.Get(entry, backend.SecretValue) - if err != nil { - return wrapped("unable to get entry", err) - } - if existing == nil { - return errors.New("entry not found") - } - if isShow { - fmt.Println(existing.Value) - return nil - } - if err := clipboard.CopyTo(existing.Value); err != nil { - return wrapped("clipboard operation failed", err) - } + return commands.ShowClip(os.Stdout, t, command == cli.ShowCommand, sub) default: return fmt.Errorf("unknown command: %s", command) } diff --git a/internal/commands/showclip.go b/internal/commands/showclip.go @@ -0,0 +1,42 @@ +// Package commands can show/clip an entry +package commands + +import ( + "errors" + "fmt" + "io" + + "github.com/enckse/lockbox/internal/backend" + "github.com/enckse/lockbox/internal/platform" +) + +// ShowClip will handle showing/clipping an entry +func ShowClip(w io.Writer, t *backend.Transaction, isShow bool, args []string) error { + if len(args) != 1 { + return errors.New("entry required") + } + entry := args[0] + clipboard := platform.Clipboard{} + if !isShow { + var err error + clipboard, err = platform.NewClipboard() + if err != nil { + return fmt.Errorf("unable to get clipboard: %w", err) + } + } + existing, err := t.Get(entry, backend.SecretValue) + if err != nil { + return err + } + if existing == nil { + return nil + } + if isShow { + fmt.Fprintln(w, existing.Value) + return nil + } + if err := clipboard.CopyTo(existing.Value); err != nil { + return fmt.Errorf("clipboard operation failed: %w", err) + } + return nil +} diff --git a/internal/commands/showclip_test.go b/internal/commands/showclip_test.go @@ -0,0 +1,38 @@ +package commands_test + +import ( + "bytes" + "os" + "testing" + + "github.com/enckse/lockbox/internal/backend" + "github.com/enckse/lockbox/internal/commands" +) + +func TestShowClip(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 b bytes.Buffer + if err := commands.ShowClip(&b, tx, true, []string{}); err.Error() != "entry required" { + t.Errorf("invalid error: %v", err) + } + if err := commands.ShowClip(&b, tx, true, []string{"test/test2/test1"}); err != nil { + t.Errorf("invalid error: %v", err) + } + if b.String() == "" { + t.Error("no show") + } + b = bytes.Buffer{} + if err := commands.ShowClip(&b, tx, true, []string{"tsest/test2/test1"}); err != nil { + t.Errorf("invalid error: %v", err) + } + if b.String() != "" { + t.Error("no show") + } + os.Clearenv() + if err := commands.ShowClip(&b, tx, false, []string{"tsest/test2/test1"}); err == nil { + t.Errorf("invalid error: %v", err) + } +}