/
tee_stream.go
46 lines (36 loc) · 882 Bytes
/
tee_stream.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
package system
import (
"github.com/sirupsen/logrus"
"io"
"os"
)
type teeReader struct {
source io.ReadCloser
output io.WriteCloser
outputPath string
}
func NewTeeReader(source io.ReadCloser, outPath string) (io.ReadCloser, error) {
f, err := os.OpenFile(outPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
return &teeReader{
source: source,
output: f,
outputPath: outPath,
}, nil
}
func (t *teeReader) Read(p []byte) (n int, err error) {
n, err = t.source.Read(p)
_, wErr := t.output.Write(p[0:n])
if wErr != nil {
logrus.WithField("file", t.outputPath).WithError(err).Warnf("Unable to write stream to output file!")
}
return n, err
}
func (t *teeReader) Close() error {
defer t.output.Close()
// copy the potential rest of stream to file!
io.Copy(t.output, t.source)
return t.source.Close()
}