lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 502249d4e08501ca95309a4c15d7bfd04f57785c
parent cce63097ccdb2bae0c9945eb4132f9837a69dd1c
Author: Sean Enck <sean@ttypty.com>
Date:   Sun,  2 Oct 2022 12:59:45 -0400

better error handling in cli

Diffstat:
Mcmd/main.go | 11++++++++++-
Minternal/backend/query.go | 4++++
Minternal/backend/query_test.go | 5++++-
3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go @@ -136,7 +136,7 @@ func run() *programError { entry := args[idx] existing, err := t.Get(entry, backend.BlankValue) if err != nil { - return newError("unable to find an exact, existing match", err) + return newError("unable to insert entry", err) } if existing != nil { if !isPipe { @@ -155,6 +155,9 @@ func run() *programError { } fmt.Println("") case "rm": + if len(args) != 3 { + return newError("rm requires a single entry", errors.New("missing argument")) + } deleting := args[2] existing, err := t.Get(deleting, backend.BlankValue) if err != nil { @@ -167,6 +170,9 @@ func run() *programError { } case "show", "clip": + if len(args) != 3 { + return newError("requires a single entry", fmt.Errorf("%s missing argument", command)) + } entry := args[2] clipboard := platform.Clipboard{} isShow := command == "show" @@ -191,6 +197,9 @@ func run() *programError { return newError("clipboard failed", err) } default: + if len(args) < 2 { + return newError("command missing required arguments", fmt.Errorf("%s missing argument", command)) + } a := args[2:] callback := internalCallback(command) if callback != nil { diff --git a/internal/backend/query.go b/internal/backend/query.go @@ -15,6 +15,10 @@ import ( // Get will request a singular entity func (t *Transaction) Get(path string, mode ValueMode) (*QueryEntity, error) { + _, _, err := splitComponents(path) + if err != nil { + return nil, err + } e, err := t.QueryCallback(QueryOptions{Mode: ExactMode, Criteria: path, Values: mode}) if err != nil { return nil, err diff --git a/internal/backend/query_test.go b/internal/backend/query_test.go @@ -24,10 +24,13 @@ func TestGet(t *testing.T) { if q.Path != "test/test/abc" || q.Value != "" { t.Error("invalid query result") } - q, err = fullSetup(t, true).Get("aaaa", backend.BlankValue) + q, err = fullSetup(t, true).Get("a/b/aaaa", backend.BlankValue) if err != nil || q != nil { t.Error("invalid result, should be empty") } + if _, err := fullSetup(t, true).Get("aaaa", backend.BlankValue); err.Error() != "input paths must contain at LEAST 3 components (e.g. abc/123/xyz)" { + t.Errorf("invalid error: %v", err) + } } func TestValueModes(t *testing.T) {