/
cleanstack.go
58 lines (51 loc) · 1.72 KB
/
cleanstack.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package utils
import (
"github.com/hashicorp/go-multierror"
)
type CleanJob func() error
// NewCleanStack returns a new stack.
// It's used to push jobs into it that need to be executed in order, like unmounting disks or removing dirs, and it
// will run those jobs in the order they were pushed into it to maintain order
// So you can create a dir, push its removal into the stack, mount something into that dir and push its unmounting into
// the stack and when cleanup is triggered it will first unmount and then remove the dir
// Usually its setup inside a function with a defer immediately so it auto cleans if you return from anywhere in the function
// That way you don't need to track on each return what needs to be cleaned and whatnot
// cleanup := utils.NewCleanStack()
// defer func() { err = cleanup.Cleanup(err) }()
func NewCleanStack() *CleanStack {
return &CleanStack{}
}
// CleanStack is a basic LIFO stack that resizes as needed.
type CleanStack struct {
jobs []CleanJob
current int
}
// Push adds a node to the stack
func (clean *CleanStack) Push(job CleanJob) {
clean.jobs = append(clean.jobs[:clean.current], job)
clean.current++
}
// Pop removes and returns a node from the stack in last to first order.
func (clean *CleanStack) Pop() CleanJob {
if clean.current == 0 {
return nil
}
clean.current--
return clean.jobs[clean.current]
}
// Cleanup runs the whole cleanup stack. In case of error it runs all jobs
// and returns the first error occurrence.
func (clean *CleanStack) Cleanup(err error) error {
var errs error
if err != nil {
errs = multierror.Append(errs, err)
}
for clean.current > 0 {
job := clean.Pop()
err = job()
if err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}