commit 1fe8a898b244d12c23d6069b857ddb8910a4cd16
parent a3cdea44afba0d945053322c4d48a77af2c767a5
Author: Sean Enck <sean@ttypty.com>
Date: Thu, 2 Mar 2023 19:43:36 -0500
moved Move
Diffstat:
3 files changed, 73 insertions(+), 24 deletions(-)
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -75,30 +75,7 @@ func Run() error {
case cli.ListCommand, cli.FindCommand:
return commands.ListFind(t, os.Stdout, command, sub)
case cli.MoveCommand:
- if len(args) != 4 {
- return errors.New("src/dst required for move")
- }
- src := args[2]
- dst := args[3]
- srcExists, err := t.Get(src, backend.SecretValue)
- if err != nil {
- return errors.New("unable to get source entry")
- }
- if srcExists == nil {
- return errors.New("no source object found")
- }
- dstExists, err := t.Get(dst, backend.BlankValue)
- if err != nil {
- return errors.New("unable to get destination object")
- }
- if dstExists != nil {
- if !confirm("overwrite destination") {
- return nil
- }
- }
- if err := t.Move(*srcExists, dst); err != nil {
- return wrapped("unable to move object", err)
- }
+ return commands.Move(t, sub, confirm)
case cli.InsertCommand:
multi := false
isTOTP := false
diff --git a/internal/commands/move.go b/internal/commands/move.go
@@ -0,0 +1,33 @@
+package commands
+
+import (
+ "errors"
+
+ "github.com/enckse/lockbox/internal/backend"
+)
+
+// Move is the CLI command to move entries
+func Move(t *backend.Transaction, args []string, confirm func(string) bool) error {
+ if len(args) != 2 {
+ return errors.New("src/dst required for move")
+ }
+ src := args[0]
+ dst := args[1]
+ srcExists, err := t.Get(src, backend.SecretValue)
+ if err != nil {
+ return errors.New("unable to get source entry")
+ }
+ if srcExists == nil {
+ return errors.New("no source object found")
+ }
+ dstExists, err := t.Get(dst, backend.BlankValue)
+ if err != nil {
+ return errors.New("unable to get destination object")
+ }
+ if dstExists != nil {
+ if !confirm("overwrite destination") {
+ return nil
+ }
+ }
+ return t.Move(*srcExists, dst)
+}
diff --git a/internal/commands/move_test.go b/internal/commands/move_test.go
@@ -0,0 +1,39 @@
+package commands_test
+
+import (
+ "testing"
+
+ "github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/commands"
+)
+
+type (
+ mockConfirm struct {
+ called bool
+ }
+)
+
+func (m *mockConfirm) prompt(string) bool {
+ m.called = true
+ return true
+}
+
+func TestMove(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")
+ m := mockConfirm{}
+ if err := commands.Move(fullSetup(t, true), []string{}, m.prompt); err.Error() != "src/dst required for move" {
+ t.Errorf("invalid error: %v", err)
+ }
+ if err := commands.Move(fullSetup(t, true), []string{"a", "b"}, m.prompt); err.Error() != "unable to get source entry" {
+ t.Errorf("invalid error: %v", err)
+ }
+ m.called = false
+ if err := commands.Move(fullSetup(t, true), []string{"test/test2/test1", "test/test2/test3"}, m.prompt); err != nil {
+ t.Errorf("invalid error: %v", err)
+ }
+ if !m.called {
+ t.Error("no move")
+ }
+}