diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-17 08:45:05 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-17 08:45:05 +0300 |
| commit | b53e348d89046ea8de5b82c283fb56980b53cdd8 (patch) | |
| tree | f2115ccf556dddfc99ad129522069845c53ebd21 /internal/store/data.go | |
| parent | e4671dc15d224d944fe5c3715c95793c21167443 (diff) | |
feat: add WriteBack hook on store.Data for backend-specific edit round-trip (tasks i4+l4)
Add optional WriteBack func([]byte) error field to Data struct.
ReimportAfterExport calls WriteBack(newContent) when set, otherwise
falls back to Commit. applyAction now calls s.LoadData (exported) so
the geheim and KeePass edit paths are symmetric. Tests cover nil,
non-nil, and error-propagation paths.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal/store/data.go')
| -rw-r--r-- | internal/store/data.go | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/internal/store/data.go b/internal/store/data.go index 8ab8429..1b9c562 100644 --- a/internal/store/data.go +++ b/internal/store/data.go @@ -13,12 +13,21 @@ import ( // Data holds a decrypted secret blob and the paths used to persist it. // DataPath is the absolute path to the on-disk .data file. // ExportedPath is populated by Export() and consumed by ReimportAfterExport(). +// +// WriteBack is an optional hook for backend-specific persistence. When set, +// ReimportAfterExport calls WriteBack(newContent) instead of the default +// encrypt-and-git-stage path (Commit). The KeePass backend will populate it +// with parse-fields+upsert+save. This keeps the edit command identical +// across backends. type Data struct { Content []byte DataPath string // absolute path to .data file ExportedPath string // set by Export(), used by ReimportAfterExport() - encryptor Encryptor - committer Committer + // WriteBack, when non-nil, is called by ReimportAfterExport with the + // newly read content instead of using the default Commit path. + WriteBack func([]byte) error + encryptor Encryptor + committer Committer } // loadData decrypts a .data file and returns a Data struct with Content populated. @@ -71,8 +80,12 @@ func (d *Data) Export(ctx context.Context, exportDir, destinationFile string) er } // ReimportAfterExport reads the (possibly edited) file from ExportedPath back -// into Content and then commits it. This is used by the edit workflow: export → -// user edits in external editor → reimport. +// into Content and persists the new content. This is used by the edit workflow: +// export → user edits in external editor → reimport. +// +// If WriteBack is set, it is called with the new content — allowing backends +// (e.g. KeePass) to supply their own persistence logic. Otherwise, the default +// geheim path is used: encrypt and git-stage via Commit. func (d *Data) ReimportAfterExport(ctx context.Context) error { content, err := os.ReadFile(d.ExportedPath) if err != nil { @@ -80,6 +93,11 @@ func (d *Data) ReimportAfterExport(ctx context.Context) error { } d.Content = content + + if d.WriteBack != nil { + return d.WriteBack(content) + } + return d.Commit(ctx, true) } |
