From 3489703ba75015386f20a3a467d72775b7ff7e27 Mon Sep 17 00:00:00 2001 From: Alexander Lazarenko Date: Sun, 25 May 2025 03:32:30 +0300 Subject: [PATCH] Refactor OpenFunc call to use deferred execution --- api.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/api.go b/api.go index 387c55a..28ebdca 100644 --- a/api.go +++ b/api.go @@ -155,6 +155,14 @@ func (a *Agate) SaveSnapshot(ctx context.Context, name string, parentID string) } } + defer func() { + if a.options.OpenFunc != nil { + if err := a.options.OpenFunc(a.options.BlobStore.GetActiveDir()); err != nil { + fmt.Printf("Failed to open resources after snapshot: %v\n", err) + } + } + }() + // If parentID is not provided, use the current snapshot ID if parentID == "" { parentID = a.currentSnapshotID @@ -176,13 +184,6 @@ func (a *Agate) SaveSnapshot(ctx context.Context, name string, parentID string) return "", fmt.Errorf("failed to save current snapshot ID: %w", err) } - // Call OpenFunc if provided - if a.options.OpenFunc != nil { - if err := a.options.OpenFunc(a.options.BlobStore.GetActiveDir()); err != nil { - return "", fmt.Errorf("failed to open resources after snapshot: %w", err) - } - } - return snapshot.ID, nil } @@ -227,6 +228,14 @@ func (a *Agate) RestoreSnapshotToDir(ctx context.Context, snapshotID string, dir } } + defer func() { + if a.options.OpenFunc != nil { + if err := a.options.OpenFunc(dir); err != nil { + fmt.Printf("Failed to open resources after snapshot: %v\n", err) + } + } + }() + // Extract the snapshot if err := a.manager.ExtractSnapshot(ctx, snapshotID, dir); err != nil { return fmt.Errorf("failed to extract snapshot: %w", err) @@ -242,13 +251,6 @@ func (a *Agate) RestoreSnapshotToDir(ctx context.Context, snapshotID string, dir } } - // Call OpenFunc if provided - if a.options.OpenFunc != nil { - if err := a.options.OpenFunc(dir); err != nil { - return fmt.Errorf("failed to open resources after restore: %w", err) - } - } - return nil }