-
Notifications
You must be signed in to change notification settings - Fork 0
/
readmulticloser.go
57 lines (50 loc) · 1.36 KB
/
readmulticloser.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
package rwmc
import (
"github.com/hashicorp/go-multierror"
"io"
)
// ReadMultiCloser is a wrapper fulfilling io.ReadCloser.
// It's use case is when you need to close multiple Closers when a main Reader is done with reading.
type ReadMultiCloser struct {
closers []io.Closer
reader io.Reader
}
// NewReadMultiCloser creates a new ReadMultiCloser.
// Passed arguments form a queue [reader, closers...].
//
// Usually the code looks similar to:
//
// r0,_ := os.Open("")
// r1 := unpack1.Open(r0)
// r2 := unpack2.Open(r1)
// r := NewMultiCloser(r2, r1, r0)
//
// Notice, similar thing can be achieved through:
//
// ioutils.NewWriteCloserWrapper(r, func() error {
// r2.Close()
// r1.Close()
// return r0.Close()
// })
func NewReadMultiCloser(reader io.ReadCloser, closers ...io.Closer) *ReadMultiCloser {
p := []io.Closer{reader}
p = append(p, closers...)
return &ReadMultiCloser{p, reader}
}
func (r *ReadMultiCloser) Read(p []byte) (n int, err error) {
return r.reader.Read(p)
}
// Close closes all closers starting from the last pushed.
func (r *ReadMultiCloser) Close() error {
var errors error
for _, c := range r.closers {
if err := c.Close(); err != nil {
errors = multierror.Append(errors, err)
}
}
return errors
}
func (r *ReadMultiCloser) Push(wc io.ReadCloser) {
r.reader = wc
r.closers = append([]io.Closer{wc}, r.closers...)
}