diff options
Diffstat (limited to 'internal/store/data.go')
| -rw-r--r-- | internal/store/data.go | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/internal/store/data.go b/internal/store/data.go index 25e64b1..8ab8429 100644 --- a/internal/store/data.go +++ b/internal/store/data.go @@ -17,11 +17,13 @@ type Data struct { Content []byte DataPath string // absolute path to .data file ExportedPath string // set by Export(), used by ReimportAfterExport() + encryptor Encryptor + committer Committer } // loadData decrypts a .data file and returns a Data struct with Content populated. // absoluteDataPath must be the full filesystem path to the encrypted .data file. -func loadData(ctx context.Context, absoluteDataPath string, encryptor Encryptor) (*Data, error) { +func loadData(ctx context.Context, absoluteDataPath string, encryptor Encryptor, committer Committer) (*Data, error) { ciphertext, err := os.ReadFile(absoluteDataPath) if err != nil { return nil, fmt.Errorf("reading data file %q: %w", absoluteDataPath, err) @@ -36,8 +38,10 @@ func loadData(ctx context.Context, absoluteDataPath string, encryptor Encryptor) } return &Data{ - Content: plain, - DataPath: absoluteDataPath, + Content: plain, + DataPath: absoluteDataPath, + encryptor: encryptor, + committer: committer, }, nil } @@ -69,21 +73,21 @@ 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. -func (d *Data) ReimportAfterExport(ctx context.Context, encryptor Encryptor, committer Committer) error { +func (d *Data) ReimportAfterExport(ctx context.Context) error { content, err := os.ReadFile(d.ExportedPath) if err != nil { return fmt.Errorf("reading exported file %q: %w", d.ExportedPath, err) } d.Content = content - return d.Commit(ctx, encryptor, committer, true) + return d.Commit(ctx, true) } // Commit encrypts Content and writes it to DataPath, then stages the file with git. // If force is false and the file already exists, the commit is silently skipped // (matching the Ruby CommitFile#commit_content behaviour that avoids overwrites // without explicit force). -func (d *Data) Commit(ctx context.Context, encryptor Encryptor, committer Committer, force bool) error { +func (d *Data) Commit(ctx context.Context, force bool) error { if !force { if _, err := os.Stat(d.DataPath); err == nil { // File already exists; skip without error to preserve existing data. @@ -96,10 +100,10 @@ func (d *Data) Commit(ctx context.Context, encryptor Encryptor, committer Commit return fmt.Errorf("creating data directory for %q: %w", d.DataPath, err) } - if encryptor == nil { + if d.encryptor == nil { return fmt.Errorf("encrypting data for %q: missing encryptor", d.DataPath) } - ciphertext, err := encryptor.Encrypt(d.Content) + ciphertext, err := d.encryptor.Encrypt(d.Content) if err != nil { return fmt.Errorf("encrypting data for %q: %w", d.DataPath, err) } @@ -108,10 +112,10 @@ func (d *Data) Commit(ctx context.Context, encryptor Encryptor, committer Commit return fmt.Errorf("writing data file %q: %w", d.DataPath, err) } - if committer == nil { + if d.committer == nil { return fmt.Errorf("git add data %q: missing committer", d.DataPath) } - if err := committer.Add(ctx, d.DataPath); err != nil { + if err := d.committer.Add(ctx, d.DataPath); err != nil { return fmt.Errorf("git add data %q: %w", d.DataPath, err) } |
