Refactor OpenFunc call to use deferred execution

This commit is contained in:
Александр Лазаренко 2025-05-25 03:32:30 +03:00
parent 0a9285c05e
commit 3489703ba7
Signed by: Kerblif
GPG Key ID: 5AFAD6640F4670C3

30
api.go
View File

@ -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
}