lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 7781e240d945cae261e13b1767bfe3cd57eefc41
parent 3bcbae6565b4724264dfa7141a01afe1aef21efb
Author: Sean Enck <sean@ttypty.com>
Date:   Sat, 16 Jul 2022 15:19:12 -0400

filter non empty errors in filesystem

Diffstat:
Mcmd/lb-totp/main.go | 31++++++++++---------------------
Minternal/store/filesystem.go | 8++++++--
Minternal/store/filesystem_test.go | 15+++++++++++++++
3 files changed, 31 insertions(+), 23 deletions(-)

diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go @@ -24,24 +24,6 @@ var ( mainExe = "" ) -func list() ([]string, error) { - f := store.NewFileSystemStore() - token := totpToken(f, true) - results, err := f.List(store.ViewOptions{Filter: func(path string) string { - if filepath.Base(path) == token { - return filepath.Dir(f.CleanPath(path)) - } - return "" - }}) - if err != nil { - return nil, err - } - if len(results) == 0 { - return nil, errors.New("no objects found") - } - return results, nil -} - func clear() { cmd := exec.Command("clear") cmd.Stdout = os.Stdout @@ -175,12 +157,19 @@ func main() { cmd := args[1] options := cli.ParseArgs(cmd) if options.List { - result, err := list() + f := store.NewFileSystemStore() + token := totpToken(f, true) + results, err := f.List(store.ViewOptions{ErrorOnEmpty: true, Filter: func(path string) string { + if filepath.Base(path) == token { + return filepath.Dir(f.CleanPath(path)) + } + return "" + }}) if err != nil { misc.Die("invalid list response", err) } - sort.Strings(result) - for _, entry := range result { + sort.Strings(results) + for _, entry := range results { fmt.Println(entry) } return diff --git a/internal/store/filesystem.go b/internal/store/filesystem.go @@ -24,8 +24,9 @@ type ( } // ViewOptions represent list options for parsing store entries. ViewOptions struct { - Display bool - Filter ListEntryFilter + Display bool + Filter ListEntryFilter + ErrorOnEmpty bool } ) @@ -63,6 +64,9 @@ func (s FileSystem) List(options ViewOptions) ([]string, error) { if err != nil { return nil, err } + if options.ErrorOnEmpty && len(results) == 0 { + return nil, errors.New("no results found") + } if options.Display { sort.Strings(results) } diff --git a/internal/store/filesystem_test.go b/internal/store/filesystem_test.go @@ -64,6 +64,21 @@ func TestList(t *testing.T) { if len(res) != 2 || res[0] != "1" || res[1] != "2" { t.Error("mismatch filter results") } + res, err = s.List(ViewOptions{ErrorOnEmpty: false, Filter: func(path string) string { + return "" + }}) + if err != nil { + t.Errorf("should be non-error: %v", err) + } + if len(res) != 0 { + t.Error("should be empty list") + } + _, err = s.List(ViewOptions{ErrorOnEmpty: true, Filter: func(path string) string { + return "" + }}) + if err == nil || err.Error() != "no results found" { + t.Errorf("should be non-error: %v", err) + } } func TestFileSystemFile(t *testing.T) {