/
tar.go
37 lines (32 loc) · 921 Bytes
/
tar.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
package testtools
import (
"archive/tar"
"fmt"
"github.com/pkg/errors"
"io"
"io/ioutil"
)
// BufferTarInterpreter extracts data to a byte slice. Used
// for testing purposes.
type BufferTarInterpreter struct {
Out []byte
}
// Interpret handles in memory tar formats. Used for testing purposes.
func (tarInterpreter *BufferTarInterpreter) Interpret(reader io.Reader, header *tar.Header) error {
// defer TimeTrack(time.Now(), "BUFFER INTERPRET")
// Assumes only regular files
out, err := ioutil.ReadAll(reader)
if err != nil {
return errors.Wrap(err, "Interpret: ReadAll failed")
}
tarInterpreter.Out = out
return nil
}
// NOPTarInterpreter mocks a tar extractor.
type NOPTarInterpreter struct{}
// Interpret does not do anything except print the
// 'tar member' name.
func (tarInterpreter *NOPTarInterpreter) Interpret(tr io.Reader, header *tar.Header) error {
fmt.Println(header.Name)
return nil
}