lockbox

password manager
Log | Files | Refs | README | LICENSE

commit f10fae07b991a13140008f587725bf4549c60e33
parent 897b722d12dfa8dbffd0f6d57bb7686c0f1d6fc7
Author: Sean Enck <sean@ttypty.com>
Date:   Sat, 16 Jul 2022 12:28:52 -0400

lists can now filter

Diffstat:
Mcmd/lb-totp/main.go | 16+++++++---------
Minternal/store/filesystem.go | 9+++++++++
Minternal/store/filesystem_test.go | 16++++++++++++++++
3 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go @@ -25,18 +25,16 @@ var ( ) func list() ([]string, error) { - files := []string{} f := store.NewFileSystemStore() - files, err := f.List(store.ViewOptions{}) - if err != nil { - return nil, err - } token := totpToken(f, true) - var results []string - for _, obj := range files { - if filepath.Base(obj) == token { - results = append(results, filepath.Dir(f.CleanPath(obj))) + 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") diff --git a/internal/store/filesystem.go b/internal/store/filesystem.go @@ -16,6 +16,8 @@ const ( ) type ( + // ListEntryFilter allows for filtering/changing view results. + ListEntryFilter func(string) string // FileSystem represents a filesystem store. FileSystem struct { path string @@ -23,6 +25,7 @@ type ( // ViewOptions represent list options for parsing store entries. ViewOptions struct { Display bool + Filter ListEntryFilter } ) @@ -46,6 +49,12 @@ func (s FileSystem) List(options ViewOptions) ([]string, error) { if options.Display { usePath = s.trim(usePath) } + if options.Filter != nil { + usePath = options.Filter(usePath) + if usePath == "" { + return nil + } + } results = append(results, usePath) } return nil diff --git a/internal/store/filesystem_test.go b/internal/store/filesystem_test.go @@ -1,8 +1,10 @@ package store import ( + "fmt" "os" "path/filepath" + "strings" "testing" "github.com/enckse/lockbox/internal/misc" @@ -48,6 +50,20 @@ func TestList(t *testing.T) { if res[0] != "aaa" || res[1] != "sub/12lkjafav" || res[2] != "sub/aaaaajk" || res[3] != "test" || res[4] != "test2" { t.Errorf("not sorted: %v", res) } + idx := 0 + res, err = s.List(ViewOptions{Filter: func(path string) string { + if strings.Contains(path, "test") { + idx++ + return fmt.Sprintf("%d", idx) + } + return "" + }}) + if err != nil { + t.Errorf("unable to list: %v", err) + } + if len(res) != 2 || res[0] != "1" || res[1] != "2" { + t.Error("mismatch filter results") + } } func TestFileSystemFile(t *testing.T) {