commit 25325851851af09664fbcd7c901585aea2f53242
parent 1fe8a898b244d12c23d6069b857ddb8910a4cd16
Author: Sean Enck <sean@ttypty.com>
Date: Thu, 2 Mar 2023 19:50:42 -0500
more migrations
Diffstat:
5 files changed, 64 insertions(+), 27 deletions(-)
diff --git a/internal/app/core.go b/internal/app/core.go
@@ -37,7 +37,7 @@ func handleEarly(command string, args []string) (bool, error) {
case cli.TOTPCommand:
return true, totp.Call(args)
case cli.HashCommand:
- return true, hashText(args)
+ return true, commands.Hash(os.Stdout, args)
case cli.ClearCommand:
return true, clearClipboard(args)
}
@@ -73,7 +73,7 @@ func Run() error {
return t.ReKey()
}
case cli.ListCommand, cli.FindCommand:
- return commands.ListFind(t, os.Stdout, command, sub)
+ return commands.ListFind(t, os.Stdout, command == cli.FindCommand, sub)
case cli.MoveCommand:
return commands.Move(t, sub, confirm)
case cli.InsertCommand:
@@ -203,24 +203,6 @@ func Run() error {
return nil
}
-func hashText(args []string) error {
- if len(args) == 0 {
- return errors.New("hash requires a file")
- }
- t, err := backend.Load(args[len(args)-1])
- if err != nil {
- return err
- }
- e, err := t.QueryCallback(backend.QueryOptions{Mode: backend.ListMode, Values: backend.HashedValue})
- if err != nil {
- return err
- }
- for _, item := range e {
- fmt.Printf("%s:\n %s\n\n", item.Path, strings.ReplaceAll(item.Value, "\n", "\n "))
- }
- return nil
-}
-
func clearClipboard(args []string) error {
idx := 0
val, err := inputs.Stdin(false)
diff --git a/internal/commands/hash.go b/internal/commands/hash.go
@@ -0,0 +1,31 @@
+package commands
+
+import (
+ "errors"
+ "fmt"
+ "io"
+ "strings"
+
+ "github.com/enckse/lockbox/internal/backend"
+)
+
+// Hash will hash 1-N files
+func Hash(w io.Writer, args []string) error {
+ if len(args) == 0 {
+ return errors.New("hash requires a file")
+ }
+ for _, a := range args {
+ t, err := backend.Load(a)
+ if err != nil {
+ return err
+ }
+ e, err := t.QueryCallback(backend.QueryOptions{Mode: backend.ListMode, Values: backend.HashedValue})
+ if err != nil {
+ return err
+ }
+ for _, item := range e {
+ fmt.Fprintf(w, "%s:\n %s\n\n", item.Path, strings.ReplaceAll(item.Value, "\n", "\n "))
+ }
+ }
+ return nil
+}
diff --git a/internal/commands/hash_test.go b/internal/commands/hash_test.go
@@ -0,0 +1,25 @@
+package commands_test
+
+import (
+ "bytes"
+ "testing"
+
+ "github.com/enckse/lockbox/internal/backend"
+ "github.com/enckse/lockbox/internal/commands"
+)
+
+func TestHash(t *testing.T) {
+ var buf bytes.Buffer
+ if err := commands.Hash(&buf, []string{}); err.Error() != "hash requires a file" {
+ t.Errorf("invalid error: %v", err)
+ }
+ setup(t)
+ fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test1"), "pass")
+ fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test3"), "pass")
+ if err := commands.Hash(&buf, []string{"test.kdbx"}); err != nil {
+ t.Errorf("invalid error: %v", err)
+ }
+ if buf.String() == "" {
+ t.Error("nothing hashed")
+ }
+}
diff --git a/internal/commands/listfind.go b/internal/commands/listfind.go
@@ -6,14 +6,13 @@ import (
"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 {
+func ListFind(t *backend.Transaction, w io.Writer, isFind bool, args []string) error {
opts := backend.QueryOptions{}
opts.Mode = backend.ListMode
- if command == cli.FindCommand {
+ if isFind {
opts.Mode = backend.FindMode
if len(args) < 1 {
return errors.New("find requires search term")
diff --git a/internal/commands/listfind_test.go b/internal/commands/listfind_test.go
@@ -39,13 +39,13 @@ func TestList(t *testing.T) {
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 {
+ if err := commands.ListFind(tx, &buf, false, []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" {
+ if err := commands.ListFind(tx, &buf, false, []string{"test"}); err.Error() != "list does not support any arguments" {
t.Errorf("invalid error: %v", err)
}
}
@@ -56,10 +56,10 @@ func TestFind(t *testing.T) {
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" {
+ if err := commands.ListFind(tx, &buf, true, []string{}); err.Error() != "find requires search term" {
t.Errorf("invalid error: %v", err)
}
- if err := commands.ListFind(tx, &buf, "find", []string{"test1"}); err != nil {
+ if err := commands.ListFind(tx, &buf, true, []string{"test1"}); err != nil {
t.Errorf("invalid error: %v", err)
}
if buf.String() == "" || strings.Contains(buf.String(), "test3") {