lockbox

password manager
Log | Files | Refs | README | LICENSE

commit b4af1807ab02cc244c85bb1dfd01991a10a559bc
parent 200df5997f9787dea06a7f7bf10c4a52fd4d5d64
Author: Sean Enck <sean@ttypty.com>
Date:   Sat, 16 Jul 2022 11:05:29 -0400

internal cleanup mostly completed

Diffstat:
Minternal/colors/terminal.go | 3++-
Minternal/dump/export.go | 1+
Minternal/encrypt/core.go | 4++--
Minternal/encrypt/core_test.go | 9+++++----
Minternal/hooks/execute.go | 11++++++-----
Minternal/inputs/env.go | 1+
Ainternal/store/filesystem.go | 64++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainternal/store/filesystem_test.go | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Dinternal/store/store.go | 63---------------------------------------------------------------
Dinternal/store/store_test.go | 50--------------------------------------------------
10 files changed, 132 insertions(+), 125 deletions(-)

diff --git a/internal/colors/terminal.go b/internal/colors/terminal.go @@ -2,6 +2,7 @@ package colors import ( "errors" + "github.com/enckse/lockbox/internal/inputs" ) @@ -29,7 +30,7 @@ func NewTerminal(color Color) (Terminal, error) { } colors := interactive if colors { - isColored, err := inputs.IsColorEnabled() + isColored, err := inputs.IsNoColorEnabled() if err != nil { return Terminal{}, err } diff --git a/internal/dump/export.go b/internal/dump/export.go @@ -12,6 +12,7 @@ type ( } ) +// Marshal handles marshalling of entities to output formats. func Marshal(entities []ExportEntity) ([]byte, error) { return json.MarshalIndent(entities, "", " ") } diff --git a/internal/encrypt/core.go b/internal/encrypt/core.go @@ -10,9 +10,9 @@ import ( "strings" "time" + "github.com/enckse/lockbox/internal/inputs" "github.com/google/shlex" "golang.org/x/crypto/nacl/secretbox" - "github.com/enckse/lockbox/internal/inputs" ) const ( @@ -117,7 +117,7 @@ func (l Lockbox) Encrypt(datum []byte) error { } data := datum if data == nil { - b, err := inputs.RawStdin(false) + b, err := inputs.RawStdin() if err != nil { return err } diff --git a/internal/encrypt/core_test.go b/internal/encrypt/core_test.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" "testing" + "github.com/enckse/lockbox/internal/misc" ) @@ -12,13 +13,13 @@ func setupData(t *testing.T) string { os.Setenv("LOCKBOX_KEY", "") if misc.PathExists("bin") { if err := os.RemoveAll("bin"); err != nil { - t.Errorf("unable to cleanup dir: %v") + t.Errorf("unable to cleanup dir: %v", err) } } - if err := os.MkdirAll("bin", 0755); err != nil { - t.Errorf("failed to setup bin directory: %v", err) - } + if err := os.MkdirAll("bin", 0755); err != nil { + t.Errorf("failed to setup bin directory: %v", err) + } return filepath.Join("bin", "test.lb") } diff --git a/internal/hooks/execute.go b/internal/hooks/execute.go @@ -5,22 +5,23 @@ import ( "os" "os/exec" "path/filepath" + "github.com/enckse/lockbox/internal/misc" ) type ( - // HookAction are specific steps that may call a hook. + // Action are specific steps that may call a hook. Action string - // HookStep is the step, during command execution, when the hook was called. + // Step is the step, during command execution, when the hook was called. Step string ) const ( - // RemoveHook is called when a store entry is removed. + // Remove is called when a store entry is removed. Remove Action = "remove" - // InsertHook is called when a store entry is inserted. + // Insert is called when a store entry is inserted. Insert Action = "insert" - // PostHookStep is a hook running at the end of a command. + // PostStep is a hook running at the end of a command. PostStep Step = "post" ) diff --git a/internal/inputs/env.go b/internal/inputs/env.go @@ -20,6 +20,7 @@ func isYesNoEnv(defaultValue bool, env string) (bool, error) { return false, fmt.Errorf("invalid yes/no env value for %s", env) } +// IsNoColorEnabled indicates if the flag is set to disable color. func IsNoColorEnabled() (bool, error) { return isYesNoEnv(false, "LOCKBOX_NOCOLOR") } diff --git a/internal/store/filesystem.go b/internal/store/filesystem.go @@ -0,0 +1,64 @@ +package store + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "sort" + "strings" + + "github.com/enckse/lockbox/internal/misc" +) + +const ( + // Extension is the lockbox file extension. + Extension = ".lb" +) + +type ( + // FileSystem represents a filesystem store. + FileSystem struct { + path string + } + // ViewOptions represent list options for parsing store entries. + ViewOptions struct { + Display bool + } +) + +// NewFileSystemStore gets the lockbox directory (filesystem-based) store. +func NewFileSystemStore() string { + return os.Getenv("LOCKBOX_STORE") +} + +// List will get all lockbox files in a store. +func (s FileSystem) List(options ViewOptions) ([]string, error) { + var results []string + if !misc.PathExists(s.path) { + return nil, errors.New("store does not exist") + } + err := filepath.Walk(s.path, func(path string, info fs.FileInfo, err error) error { + if err != nil { + return err + } + if strings.HasSuffix(path, Extension) { + usePath := path + if options.Display { + usePath = strings.TrimPrefix(usePath, s.path) + usePath = strings.TrimPrefix(usePath, "/") + usePath = strings.TrimSuffix(usePath, Extension) + } + results = append(results, usePath) + } + return nil + }) + + if err != nil { + return nil, err + } + if options.Display { + sort.Strings(results) + } + return results, nil +} diff --git a/internal/store/filesystem_test.go b/internal/store/filesystem_test.go @@ -0,0 +1,51 @@ +package store + +import ( + "os" + "path/filepath" + "testing" + + "github.com/enckse/lockbox/internal/misc" +) + +func TestListErrors(t *testing.T) { + _, err := FileSystem{path: "aaa"}.List(ViewOptions{}) + if err == nil || err.Error() != "store does not exist" { + t.Errorf("invalid store error: %v", err) + } +} + +func TestList(t *testing.T) { + testStore := "bin" + if misc.PathExists(testStore) { + if err := os.RemoveAll(testStore); err != nil { + t.Errorf("invalid error on remove: %v", err) + } + } + if err := os.MkdirAll(filepath.Join(testStore, "sub"), 0755); err != nil { + t.Errorf("unable to makedir: %v", err) + } + for _, path := range []string{"test", "test2", "aaa", "sub/aaaaajk", "sub/12lkjafav"} { + if err := os.WriteFile(filepath.Join(testStore, path+Extension), []byte(""), 0644); err != nil { + t.Errorf("failed to write %s: %v", path, err) + } + } + s := FileSystem{path: testStore} + res, err := s.List(ViewOptions{}) + if err != nil { + t.Errorf("unable to list: %v", err) + } + if len(res) != 5 { + t.Error("mismatched results") + } + res, err = s.List(ViewOptions{Display: true}) + if err != nil { + t.Errorf("unable to list: %v", err) + } + if len(res) != 5 { + t.Error("mismatched results") + } + if res[0] != "aaa" || res[1] != "sub/12lkjafav" || res[2] != "sub/aaaaajk" || res[3] != "test" || res[4] != "test2" { + t.Errorf("not sorted: %v", res) + } +} diff --git a/internal/store/store.go b/internal/store/store.go @@ -1,63 +0,0 @@ -package store - -import ( - "errors" - "io/fs" - "os" - "path/filepath" - "sort" - "strings" - "github.com/enckse/lockbox/internal/misc" -) - -const ( - // Extension is the lockbox file extension. - Extension = ".lb" -) - -type ( - // FileSystem represents a filesystem store. - FileSystem struct { - path string - } - ViewOptions struct { - Display bool - } - -) - -// NewFileSystemStore gets the lockbox directory (filesystem-based) store. -func NewFileSystemStore() string { - return os.Getenv("LOCKBOX_STORE") -} - -// List will get all lockbox files in a store. -func (s FileSystem) List(options ViewOptions) ([]string, error) { - var results []string - if !misc.PathExists(s.path) { - return nil, errors.New("store does not exist") - } - err := filepath.Walk(s.path, func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err - } - if strings.HasSuffix(path, Extension) { - usePath := path - if options.Display { - usePath = strings.TrimPrefix(usePath, s.path) - usePath = strings.TrimPrefix(usePath, "/") - usePath = strings.TrimSuffix(usePath, Extension) - } - results = append(results, usePath) - } - return nil - }) - - if err != nil { - return nil, err - } - if options.Display { - sort.Strings(results) - } - return results, nil -} diff --git a/internal/store/store_test.go b/internal/store/store_test.go @@ -1,50 +0,0 @@ -package store - -import ( - "os" - "path/filepath" - "testing" - "github.com/enckse/lockbox/internal/misc" -) - -func TestListErrors(t *testing.T) { - _, err := FileSystem{path: "aaa"}.List(ViewOptions{}) - if err == nil || err.Error() != "store does not exist" { - t.Errorf("invalid store error: %v", err) - } -} - -func TestList(t *testing.T) { - testStore := "bin" - if misc.PathExists(testStore) { - if err := os.RemoveAll(testStore); err != nil { - t.Errorf("invalid error on remove: %v", err) - } - } - if err := os.MkdirAll(filepath.Join(testStore, "sub"), 0755); err != nil { - t.Errorf("unable to makedir: %v", err) - } - for _, path := range []string{"test", "test2", "aaa", "sub/aaaaajk", "sub/12lkjafav"} { - if err := os.WriteFile(filepath.Join(testStore, path+Extension), []byte(""), 0644); err != nil { - t.Errorf("failed to write %s: %v", path, err) - } - } - s := FileSystem{path: testStore} - res, err := s.List(ViewOptions{}) - if err != nil { - t.Errorf("unable to list: %v", err) - } - if len(res) != 5 { - t.Error("mismatched results") - } - res, err = s.List(ViewOptions{Display: true}) - if err != nil { - t.Errorf("unable to list: %v", err) - } - if len(res) != 5 { - t.Error("mismatched results") - } - if res[0] != "aaa" || res[1] != "sub/12lkjafav" || res[2] != "sub/aaaaajk" || res[3] != "test" || res[4] != "test2" { - t.Errorf("not sorted: %v", res) - } -}