commit bbe79460cc0b6bfa4a2293bf672cbf2928672664
parent 44f8efd6703c4a3b2fca341fb8886eed39c86368
Author: Sean Enck <sean@ttypty.com>
Date: Mon, 10 Oct 2022 19:54:52 -0400
no longer use filepath/os.PathSep as this is no longer pathing in general
Diffstat:
5 files changed, 54 insertions(+), 38 deletions(-)
diff --git a/internal/backend/actions.go b/internal/backend/actions.go
@@ -4,7 +4,6 @@ package backend
import (
"errors"
"os"
- "path/filepath"
"strings"
"github.com/enckse/lockbox/internal/inputs"
@@ -154,8 +153,8 @@ func splitComponents(path string) ([]string, string, error) {
if len(strings.Split(path, pathSep)) < 2 {
return nil, "", errPath
}
- title := filepath.Base(path)
- parts := strings.Split(filepath.Dir(path), pathSep)
+ title := base(path)
+ parts := strings.Split(directory(path), pathSep)
return parts, title, nil
}
diff --git a/internal/backend/actions_test.go b/internal/backend/actions_test.go
@@ -3,7 +3,6 @@ package backend_test
import (
"fmt"
"os"
- "path/filepath"
"testing"
"github.com/enckse/lockbox/internal/backend"
@@ -55,22 +54,22 @@ func TestBadAction(t *testing.T) {
func TestMove(t *testing.T) {
setup(t)
- fullSetup(t, true).Insert(filepath.Join("test", "test2", "test1"), "pass")
- fullSetup(t, true).Insert(filepath.Join("test", "test2", "test3"), "pass")
- if err := fullSetup(t, true).Move(backend.QueryEntity{Path: filepath.Join("test", "test2", "test3"), Value: "abc"}, filepath.Join("test1", "test2", "test3")); err != nil {
+ fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test1"), "pass")
+ fullSetup(t, true).Insert(backend.NewPath("test", "test2", "test3"), "pass")
+ if err := fullSetup(t, true).Move(backend.QueryEntity{Path: backend.NewPath("test", "test2", "test3"), Value: "abc"}, backend.NewPath("test1", "test2", "test3")); err != nil {
t.Errorf("no error: %v", err)
}
- q, err := fullSetup(t, true).Get(filepath.Join("test1", "test2", "test3"), backend.SecretValue)
+ q, err := fullSetup(t, true).Get(backend.NewPath("test1", "test2", "test3"), backend.SecretValue)
if err != nil {
t.Errorf("no error: %v", err)
}
if q.Value != "abc" {
t.Errorf("invalid retrieval")
}
- if err := fullSetup(t, true).Move(backend.QueryEntity{Path: filepath.Join("test", "test2", "test1"), Value: "test"}, filepath.Join("test1", "test2", "test3")); err != nil {
+ if err := fullSetup(t, true).Move(backend.QueryEntity{Path: backend.NewPath("test", "test2", "test1"), Value: "test"}, backend.NewPath("test1", "test2", "test3")); err != nil {
t.Errorf("no error: %v", err)
}
- q, err = fullSetup(t, true).Get(filepath.Join("test1", "test2", "test3"), backend.SecretValue)
+ q, err = fullSetup(t, true).Get(backend.NewPath("test1", "test2", "test3"), backend.SecretValue)
if err != nil {
t.Errorf("no error: %v", err)
}
@@ -92,30 +91,30 @@ func TestInserts(t *testing.T) {
if err := setup(t).Insert("a", ""); err.Error() != "empty secret not allowed" {
t.Errorf("wrong error: %v", err)
}
- if err := setup(t).Insert(filepath.Join("test", "offset", "value"), "pass"); err != nil {
+ if err := setup(t).Insert(backend.NewPath("test", "offset", "value"), "pass"); err != nil {
t.Errorf("no error: %v", err)
}
- if err := fullSetup(t, true).Insert(filepath.Join("test", "offset", "value"), "pass2"); err != nil {
+ if err := fullSetup(t, true).Insert(backend.NewPath("test", "offset", "value"), "pass2"); err != nil {
t.Errorf("wrong error: %v", err)
}
- if err := fullSetup(t, true).Insert(filepath.Join("test", "offset", "value2"), "pass\npass"); err != nil {
+ if err := fullSetup(t, true).Insert(backend.NewPath("test", "offset", "value2"), "pass\npass"); err != nil {
t.Errorf("no error: %v", err)
}
- q, err := fullSetup(t, true).Get(filepath.Join("test", "offset", "value"), backend.SecretValue)
+ q, err := fullSetup(t, true).Get(backend.NewPath("test", "offset", "value"), backend.SecretValue)
if err != nil {
t.Errorf("no error: %v", err)
}
if q.Value != "pass2" {
t.Errorf("invalid retrieval")
}
- q, err = fullSetup(t, true).Get(filepath.Join("test", "offset", "value2"), backend.SecretValue)
+ q, err = fullSetup(t, true).Get(backend.NewPath("test", "offset", "value2"), backend.SecretValue)
if err != nil {
t.Errorf("no error: %v", err)
}
if q.Value != "pass\npass" {
t.Errorf("invalid retrieval")
}
- if err := fullSetup(t, true).Insert(filepath.Join("test", "offset", "totp"), "5ae472abqdekjqykoyxk7hvc2leklq5n"); err != nil {
+ if err := fullSetup(t, true).Insert(backend.NewPath("test", "offset", "totp"), "5ae472abqdekjqykoyxk7hvc2leklq5n"); err != nil {
t.Errorf("no error: %v", err)
}
}
@@ -127,48 +126,48 @@ func TestRemoves(t *testing.T) {
if err := setup(t).Remove(&backend.QueryEntity{}); err.Error() != "input paths must contain at LEAST 2 components" {
t.Errorf("wrong error: %v", err)
}
- if err := setup(t).Remove(&backend.QueryEntity{Path: filepath.Join("test1", "test2", "test3")}); err.Error() != "failed to remove entity" {
+ if err := setup(t).Remove(&backend.QueryEntity{Path: backend.NewPath("test1", "test2", "test3")}); err.Error() != "failed to remove entity" {
t.Errorf("wrong error: %v", err)
}
setup(t)
for _, i := range []string{"test1", "test2"} {
- fullSetup(t, true).Insert(filepath.Join(i, i, i), "pass")
+ fullSetup(t, true).Insert(backend.NewPath(i, i, i), "pass")
}
- if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: filepath.Join("test1", "test1", "test1")}); err != nil {
+ if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: backend.NewPath("test1", "test1", "test1")}); err != nil {
t.Errorf("wrong error: %v", err)
}
- if err := check(t, filepath.Join("test2", "test2", "test2")); err != nil {
+ if err := check(t, backend.NewPath("test2", "test2", "test2")); err != nil {
t.Errorf("invalid check: %v", err)
}
- if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: filepath.Join("test2", "test2", "test2")}); err != nil {
+ if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: backend.NewPath("test2", "test2", "test2")}); err != nil {
t.Errorf("wrong error: %v", err)
}
setup(t)
- for _, i := range []string{filepath.Join("test", "test", "test1"), filepath.Join("test", "test", "test2"), filepath.Join("test", "test", "test3"), filepath.Join("test", "test1", "test2"), filepath.Join("test", "test1", "test5")} {
+ for _, i := range []string{backend.NewPath("test", "test", "test1"), backend.NewPath("test", "test", "test2"), backend.NewPath("test", "test", "test3"), backend.NewPath("test", "test1", "test2"), backend.NewPath("test", "test1", "test5")} {
fullSetup(t, true).Insert(i, "pass")
}
if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: "test/test/test3"}); err != nil {
t.Errorf("wrong error: %v", err)
}
- if err := check(t, filepath.Join("test", "test", "test2"), filepath.Join("test", "test", "test1"), filepath.Join("test", "test1", "test2"), filepath.Join("test", "test1", "test5")); err != nil {
+ if err := check(t, backend.NewPath("test", "test", "test2"), backend.NewPath("test", "test", "test1"), backend.NewPath("test", "test1", "test2"), backend.NewPath("test", "test1", "test5")); err != nil {
t.Errorf("invalid check: %v", err)
}
if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: "test/test/test1"}); err != nil {
t.Errorf("wrong error: %v", err)
}
- if err := check(t, filepath.Join("test", "test", "test2"), filepath.Join("test", "test1", "test2"), filepath.Join("test", "test1", "test5")); err != nil {
+ if err := check(t, backend.NewPath("test", "test", "test2"), backend.NewPath("test", "test1", "test2"), backend.NewPath("test", "test1", "test5")); err != nil {
t.Errorf("invalid check: %v", err)
}
if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: "test/test1/test5"}); err != nil {
t.Errorf("wrong error: %v", err)
}
- if err := check(t, filepath.Join("test", "test", "test2"), filepath.Join("test", "test1", "test2")); err != nil {
+ if err := check(t, backend.NewPath("test", "test", "test2"), backend.NewPath("test", "test1", "test2")); err != nil {
t.Errorf("invalid check: %v", err)
}
if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: "test/test1/test2"}); err != nil {
t.Errorf("wrong error: %v", err)
}
- if err := check(t, filepath.Join("test", "test", "test2")); err != nil {
+ if err := check(t, backend.NewPath("test", "test", "test2")); err != nil {
t.Errorf("invalid check: %v", err)
}
if err := fullSetup(t, true).Remove(&backend.QueryEntity{Path: "test/test/test2"}); err != nil {
diff --git a/internal/backend/query.go b/internal/backend/query.go
@@ -5,8 +5,6 @@ import (
"crypto/sha512"
"errors"
"fmt"
- "os"
- "path/filepath"
"sort"
"strings"
@@ -39,7 +37,7 @@ func forEach(offset string, groups []gokeepasslib.Group, entries []gokeepasslib.
if offset == "" {
o = g.Name
} else {
- o = filepath.Join(offset, g.Name)
+ o = NewPath(offset, g.Name)
}
forEach(o, g.Groups, g.Entries, cb)
}
@@ -61,7 +59,7 @@ func (t *Transaction) QueryCallback(args QueryOptions) ([]QueryEntity, error) {
forEach("", ctx.db.Content.Root.Groups[0].Groups, ctx.db.Content.Root.Groups[0].Entries, func(offset string, entry gokeepasslib.Entry) {
path := getPathName(entry)
if offset != "" {
- path = filepath.Join(offset, path)
+ path = NewPath(offset, path)
}
if isSort {
switch args.Mode {
@@ -119,15 +117,25 @@ func (t *Transaction) QueryCallback(args QueryOptions) ([]QueryEntity, error) {
// NewSuffix creates a new user 'name' suffix
func NewSuffix(name string) string {
- return fmt.Sprintf("%c%s", os.PathSeparator, name)
+ return fmt.Sprintf("%s%s", pathSep, name)
}
// NewPath creates a new storage location path.
func NewPath(segments ...string) string {
- return filepath.Join(segments...)
+ return strings.Join(segments, pathSep)
}
// Directory gets the offset location of the entry without the 'name'
func (e QueryEntity) Directory() string {
- return filepath.Dir(e.Path)
+ return directory(e.Path)
+}
+
+func base(s string) string {
+ parts := strings.Split(s, pathSep)
+ return parts[len(parts)-1]
+}
+
+func directory(s string) string {
+ parts := strings.Split(s, pathSep)
+ return NewPath(parts[0 : len(parts)-1]...)
}
diff --git a/internal/backend/query_test.go b/internal/backend/query_test.go
@@ -1,7 +1,6 @@
package backend_test
import (
- "path/filepath"
"testing"
"github.com/enckse/lockbox/internal/backend"
@@ -113,15 +112,27 @@ func TestQueryCallback(t *testing.T) {
}
func TestEntityDir(t *testing.T) {
- q := backend.QueryEntity{Path: filepath.Join("abc", "xyz")}
+ q := backend.QueryEntity{Path: backend.NewPath("abc", "xyz")}
if q.Directory() != "abc" {
t.Error("invalid query directory")
}
+ q = backend.QueryEntity{Path: backend.NewPath("abc", "xyz", "111")}
+ if q.Directory() != "abc/xyz" {
+ t.Error("invalid query directory")
+ }
+ q = backend.QueryEntity{Path: ""}
+ if q.Directory() != "" {
+ t.Error("invalid query directory")
+ }
+ q = backend.QueryEntity{Path: backend.NewPath("abc")}
+ if q.Directory() != "" {
+ t.Error("invalid query directory")
+ }
}
func TestNewPath(t *testing.T) {
p := backend.NewPath("abc", "xyz")
- if p != filepath.Join("abc", "xyz") {
+ if p != backend.NewPath("abc", "xyz") {
t.Error("invalid new path")
}
}
diff --git a/internal/backend/types.go b/internal/backend/types.go
@@ -3,7 +3,6 @@ package backend
import (
"errors"
- "os"
"github.com/tobischo/gokeepasslib/v3"
)
@@ -65,7 +64,7 @@ const (
notesKey = "Notes"
titleKey = "Title"
passKey = "Password"
- pathSep = string(os.PathSeparator)
+ pathSep = "/"
)
var (