-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanup.go
32 lines (28 loc) · 1.04 KB
/
cleanup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package lvm
// CleanupSteps performs deferred cleanup on condition that an error
// was returned in the caller. This simplifies code where earlier
// steps need to be undone if a later step fails. It is not currently
// resilient to panics as this library is not expected to panic.
type CleanupSteps []func() error
// Add appends the given cleanup function to those that will be called
// if an error occurs.
func (fns *CleanupSteps) Add(fn func() error) {
*fns = append(*fns, fn)
}
// Unwind calls the cleanup funcions in LIFO order. It panics
// if any of them return an error as failure during recovery is
// itself unrecoverable.
func (fns *CleanupSteps) Unwind() {
// There was some error. Preform cleanup and return. If any of
// the cleanup functions return and error, we do the only
// sensible thing and panic.
for _, fn := range *fns {
defer func(clean func() error) { checkError(clean) }(fn)
}
}
// checkError calls `fn` and panics if it returns an error.
func checkError(fn func() error) {
if err := fn(); err != nil {
panic(err)
}
}