-
Notifications
You must be signed in to change notification settings - Fork 36
/
tarball_compressor.go
78 lines (61 loc) · 1.96 KB
/
tarball_compressor.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package fileutil
import (
"fmt"
"runtime"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
type tarballCompressor struct {
cmdRunner boshsys.CmdRunner
fs boshsys.FileSystem
}
func NewTarballCompressor(
cmdRunner boshsys.CmdRunner,
fs boshsys.FileSystem,
) Compressor {
return tarballCompressor{cmdRunner: cmdRunner, fs: fs}
}
func (c tarballCompressor) CompressFilesInDir(dir string) (string, error) {
return c.CompressSpecificFilesInDir(dir, []string{"."})
}
func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
if err != nil {
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
}
defer tarball.Close()
tarballPath := tarball.Name()
args := []string{"-czf", tarballPath, "-C", dir}
if runtime.GOOS == "darwin" {
args = append([]string{"--no-mac-metadata"}, args...)
}
for _, file := range files { //nolint:gosimple
args = append(args, file)
}
_, _, _, err = c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return "", bosherr.WrapError(err, "Shelling out to tar")
}
return tarballPath, nil
}
func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, options CompressorOptions) error {
sameOwnerOption := "--no-same-owner"
if options.SameOwner {
sameOwnerOption = "--same-owner"
}
args := []string{sameOwnerOption, "-xzf", tarballPath, "-C", dir}
if options.StripComponents != 0 {
args = append(args, fmt.Sprintf("--strip-components=%d", options.StripComponents))
}
if options.PathInArchive != "" {
args = append(args, options.PathInArchive)
}
_, _, _, err := c.cmdRunner.RunCommand("tar", args...)
if err != nil {
return bosherr.WrapError(err, "Shelling out to tar")
}
return nil
}
func (c tarballCompressor) CleanUp(tarballPath string) error {
return c.fs.RemoveAll(tarballPath)
}