commit 0fd9293a68255e1096c64e51f6866c8e611f8855
parent f72b84b01ad2b92f7468eaba44fe0078b5b07915
Author: Sean Enck <sean@ttypty.com>
Date: Thu, 27 Jul 2023 18:26:04 -0400
restructure backend code
Diffstat:
5 files changed, 112 insertions(+), 108 deletions(-)
diff --git a/internal/backend/actions.go b/internal/backend/actions.go
@@ -12,6 +12,26 @@ import (
"github.com/tobischo/gokeepasslib/v3/wrappers"
)
+type (
+ // ActionMode represents activities performed via transactions
+ ActionMode string
+ removal struct {
+ parts []string
+ title string
+ hook Hook
+ }
+ action func(t Context) error
+)
+
+const (
+ // MoveAction represents changes via moves, like the Move command
+ MoveAction ActionMode = "mv"
+ // InsertAction represents changes via inserts, like the Insert command
+ InsertAction ActionMode = "insert"
+ // RemoveAction represents changes via deletions, like Remove or globbed remove commands
+ RemoveAction ActionMode = "rm"
+)
+
func (t *Transaction) act(cb action) error {
if !t.valid {
return errors.New("invalid transaction")
diff --git a/internal/backend/core.go b/internal/backend/core.go
@@ -11,6 +11,38 @@ import (
"github.com/tobischo/gokeepasslib/v3"
)
+var errPath = errors.New("input paths must contain at LEAST 2 components")
+
+const (
+ notesKey = "Notes"
+ titleKey = "Title"
+ passKey = "Password"
+ pathSep = "/"
+ isGlob = pathSep + "*"
+ modTimeKey = "ModTime"
+)
+
+type (
+ // Transaction handles the overall operation of the transaction
+ Transaction struct {
+ valid bool
+ file string
+ exists bool
+ write bool
+ readonly bool
+ }
+ // Context handles operating on the underlying database
+ Context struct {
+ db *gokeepasslib.Database
+ }
+ // QueryEntity is the result of a query
+ QueryEntity struct {
+ Path string
+ Value string
+ backing gokeepasslib.Entry
+ }
+)
+
// Load will load a kdbx file for transactions
func Load(file string) (*Transaction, error) {
return loadFile(file, true)
diff --git a/internal/backend/hooks.go b/internal/backend/hooks.go
@@ -12,6 +12,25 @@ import (
"github.com/enckse/lockbox/internal/platform"
)
+type (
+ // HookMode are hook operations the user can tie to
+ HookMode string
+ // Hook represents a runnable user-defined hook
+ Hook struct {
+ path string
+ mode ActionMode
+ enabled bool
+ scripts []string
+ }
+)
+
+const (
+ // HookPre are triggers BEFORE an action is performed on an entity
+ HookPre HookMode = "pre"
+ // HookPost are triggers AFTER an action is performed on an entity
+ HookPost HookMode = "post"
+)
+
// NewHook will create a new hook type
func NewHook(path string, a ActionMode) (Hook, error) {
if strings.TrimSpace(path) == "" {
diff --git a/internal/backend/query.go b/internal/backend/query.go
@@ -13,6 +13,47 @@ import (
"github.com/tobischo/gokeepasslib/v3"
)
+type (
+ // QueryOptions indicates how to find entities
+ QueryOptions struct {
+ Mode QueryMode
+ Values ValueMode
+ Criteria string
+ }
+ // JSON is an entry as a JSON string
+ JSON struct {
+ ModTime string `json:"modtime"`
+ Data string `json:"data,omitempty"`
+ }
+ // QueryMode indicates HOW an entity will be found
+ QueryMode int
+ // ValueMode indicates what to do with the store value of the entity
+ ValueMode int
+)
+
+const (
+ // BlankValue will not decrypt secrets, empty value
+ BlankValue ValueMode = iota
+ // SecretValue will have the raw secret onboard
+ SecretValue
+ // JSONValue will show entries as a JSON payload
+ JSONValue
+)
+
+const (
+ noneMode QueryMode = iota
+ // ListMode indicates ALL entities will be listed
+ ListMode
+ // FindMode indicates a _contains_ search for an entity
+ FindMode
+ // ExactMode means an entity must MATCH the string exactly
+ ExactMode
+ // SuffixMode will look for an entity ending in a specific value
+ SuffixMode
+ // PrefixMode allows for entities starting with a specific value
+ PrefixMode
+)
+
// MatchPath will try to match 1 or more elements (more elements when globbing)
func (t *Transaction) MatchPath(path string) ([]QueryEntity, error) {
if !strings.HasSuffix(path, isGlob) {
diff --git a/internal/backend/types.go b/internal/backend/types.go
@@ -1,108 +0,0 @@
-// Package backend has types
-package backend
-
-import (
- "errors"
-
- "github.com/tobischo/gokeepasslib/v3"
-)
-
-type (
- // QueryMode indicates HOW an entity will be found
- QueryMode int
- // ValueMode indicates what to do with the store value of the entity
- ValueMode int
- // ActionMode represents activities performed via transactions
- ActionMode string
- // HookMode are hook operations the user can tie to
- HookMode string
- // QueryOptions indicates how to find entities
- QueryOptions struct {
- Mode QueryMode
- Values ValueMode
- Criteria string
- }
- // QueryEntity is the result of a query
- QueryEntity struct {
- Path string
- Value string
- backing gokeepasslib.Entry
- }
- action func(t Context) error
- // Transaction handles the overall operation of the transaction
- Transaction struct {
- valid bool
- file string
- exists bool
- write bool
- readonly bool
- }
- // Context handles operating on the underlying database
- Context struct {
- db *gokeepasslib.Database
- }
- // Hook represents a runnable user-defined hook
- Hook struct {
- path string
- mode ActionMode
- enabled bool
- scripts []string
- }
- removal struct {
- parts []string
- title string
- hook Hook
- }
- // JSON is an entry as a JSON string
- JSON struct {
- ModTime string `json:"modtime"`
- Data string `json:"data,omitempty"`
- }
-)
-
-const (
- noneMode QueryMode = iota
- // ListMode indicates ALL entities will be listed
- ListMode
- // FindMode indicates a _contains_ search for an entity
- FindMode
- // ExactMode means an entity must MATCH the string exactly
- ExactMode
- // SuffixMode will look for an entity ending in a specific value
- SuffixMode
- // PrefixMode allows for entities starting with a specific value
- PrefixMode
-)
-
-const (
- // MoveAction represents changes via moves, like the Move command
- MoveAction ActionMode = "mv"
- // InsertAction represents changes via inserts, like the Insert command
- InsertAction ActionMode = "insert"
- // RemoveAction represents changes via deletions, like Remove or globbed remove commands
- RemoveAction ActionMode = "rm"
- // HookPre are triggers BEFORE an action is performed on an entity
- HookPre HookMode = "pre"
- // HookPost are triggers AFTER an action is performed on an entity
- HookPost HookMode = "post"
-)
-
-const (
- // BlankValue will not decrypt secrets, empty value
- BlankValue ValueMode = iota
- // SecretValue will have the raw secret onboard
- SecretValue
- // JSONValue will show entries as a JSON payload
- JSONValue
-)
-
-const (
- notesKey = "Notes"
- titleKey = "Title"
- passKey = "Password"
- pathSep = "/"
- isGlob = pathSep + "*"
- modTimeKey = "ModTime"
-)
-
-var errPath = errors.New("input paths must contain at LEAST 2 components")