Skip to content

Commit 659b600

Browse files
authored
Merge pull request #129 from mdzhigarov/master
Reapply "Add NoCompression option to tarball compressor"
2 parents 6ca7d4a + 3b6e82e commit 659b600

File tree

4 files changed

+47
-17
lines changed

4 files changed

+47
-17
lines changed

fileutil/compressor_interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ type CompressorOptions struct {
44
SameOwner bool
55
PathInArchive string
66
StripComponents int
7+
NoCompression bool
78
}
89

910
type Compressor interface {
1011
// CompressFilesInDir returns path to a compressed file
11-
CompressFilesInDir(dir string) (path string, err error)
12+
CompressFilesInDir(dir string, options CompressorOptions) (path string, err error)
1213

13-
CompressSpecificFilesInDir(dir string, files []string) (path string, err error)
14+
CompressSpecificFilesInDir(dir string, files []string, options CompressorOptions) (path string, err error)
1415

1516
DecompressFileToDir(path string, dir string, options CompressorOptions) (err error)
1617

fileutil/fakes/fake_compressor.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66

77
type FakeCompressor struct {
88
CompressFilesInDirDir string
9+
CompressFilesInDirOptions boshcmd.CompressorOptions
910
CompressFilesInDirTarballPath string
1011
CompressFilesInDirErr error
1112
CompressFilesInDirCallBack func()
1213

1314
CompressSpecificFilesInDirDir string
1415
CompressSpecificFilesInDirFiles []string
16+
CompressSpecificFilesInDirOptions boshcmd.CompressorOptions
1517
CompressSpecificFilesInDirTarballPath string
1618
CompressSpecificFilesInDirErr error
1719
CompressSpecificFilesInDirCallBack func()
@@ -30,20 +32,20 @@ func NewFakeCompressor() *FakeCompressor {
3032
return &FakeCompressor{}
3133
}
3234

33-
func (fc *FakeCompressor) CompressFilesInDir(dir string) (string, error) {
35+
func (fc *FakeCompressor) CompressFilesInDir(dir string, options boshcmd.CompressorOptions) (string, error) {
3436
fc.CompressFilesInDirDir = dir
35-
37+
fc.CompressFilesInDirOptions = options
3638
if fc.CompressFilesInDirCallBack != nil {
3739
fc.CompressFilesInDirCallBack()
3840
}
3941

4042
return fc.CompressFilesInDirTarballPath, fc.CompressFilesInDirErr
4143
}
4244

43-
func (fc *FakeCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
45+
func (fc *FakeCompressor) CompressSpecificFilesInDir(dir string, files []string, options boshcmd.CompressorOptions) (string, error) {
4446
fc.CompressSpecificFilesInDirDir = dir
4547
fc.CompressSpecificFilesInDirFiles = files
46-
48+
fc.CompressSpecificFilesInDirOptions = options
4749
if fc.CompressSpecificFilesInDirCallBack != nil {
4850
fc.CompressSpecificFilesInDirCallBack()
4951
}

fileutil/tarball_compressor.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ func NewTarballCompressor(
2020
return tarballCompressor{cmdRunner: cmdRunner, fs: fs}
2121
}
2222

23-
func (c tarballCompressor) CompressFilesInDir(dir string) (string, error) {
24-
return c.CompressSpecificFilesInDir(dir, []string{"."})
23+
func (c tarballCompressor) CompressFilesInDir(dir string, options CompressorOptions) (string, error) {
24+
return c.CompressSpecificFilesInDir(dir, []string{"."}, options)
2525
}
2626

27-
func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string) (string, error) {
27+
func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string, options CompressorOptions) (string, error) {
2828
tarball, err := c.fs.TempFile("bosh-platform-disk-TarballCompressor-CompressSpecificFilesInDir")
2929
if err != nil {
3030
return "", bosherr.WrapError(err, "Creating temporary file for tarball")
@@ -34,7 +34,10 @@ func (c tarballCompressor) CompressSpecificFilesInDir(dir string, files []string
3434

3535
tarballPath := tarball.Name()
3636

37-
args := []string{"-czf", tarballPath, "-C", dir}
37+
args := []string{"-cf", tarballPath, "-C", dir}
38+
if !options.NoCompression {
39+
args = append(args, "-z")
40+
}
3841
if runtime.GOOS == "darwin" {
3942
args = append([]string{"--no-mac-metadata"}, args...)
4043
}
@@ -61,7 +64,7 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o
6164
if err != nil {
6265
return bosherr.WrapError(err, "Resolving tarball path")
6366
}
64-
args := []string{sameOwnerOption, "-xzf", resolvedTarballPath, "-C", dir}
67+
args := []string{sameOwnerOption, "-xf", resolvedTarballPath, "-C", dir}
6568
if options.StripComponents != 0 {
6669
args = append(args, fmt.Sprintf("--strip-components=%d", options.StripComponents))
6770
}

fileutil/tarball_compressor_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var _ = Describe("tarballCompressor", func() {
5353

5454
defer os.Remove(symlinkPath)
5555

56-
tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir)
56+
tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir, CompressorOptions{})
5757
Expect(err).ToNot(HaveOccurred())
5858
defer os.Remove(tgzName)
5959

@@ -94,6 +94,30 @@ var _ = Describe("tarballCompressor", func() {
9494
Expect(err).ToNot(HaveOccurred())
9595
Expect(content).To(ContainSubstring("this is other app stdout"))
9696
})
97+
98+
It("uses NoCompression option to create uncompressed tarball", func() {
99+
cmdRunner := fakesys.NewFakeCmdRunner()
100+
compressor := NewTarballCompressor(cmdRunner, fs)
101+
102+
tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir, CompressorOptions{NoCompression: true})
103+
Expect(err).ToNot(HaveOccurred())
104+
defer os.Remove(tgzName)
105+
106+
Expect(1).To(Equal(len(cmdRunner.RunCommands)))
107+
Expect(cmdRunner.RunCommands[0]).ToNot(ContainElement("-z"))
108+
})
109+
110+
It("uses compression by default when NoCompression is false", func() {
111+
cmdRunner := fakesys.NewFakeCmdRunner()
112+
compressor := NewTarballCompressor(cmdRunner, fs)
113+
114+
tgzName, err := compressor.CompressFilesInDir(testAssetsFixtureDir, CompressorOptions{NoCompression: false})
115+
Expect(err).ToNot(HaveOccurred())
116+
defer os.Remove(tgzName)
117+
118+
Expect(1).To(Equal(len(cmdRunner.RunCommands)))
119+
Expect(cmdRunner.RunCommands[0]).To(ContainElement("-z"))
120+
})
97121
})
98122

99123
Describe("CompressSpecificFilesInDir", func() {
@@ -104,7 +128,7 @@ var _ = Describe("tarballCompressor", func() {
104128
"some_directory",
105129
"app.stderr.log",
106130
}
107-
tgzName, err := compressor.CompressSpecificFilesInDir(srcDir, files)
131+
tgzName, err := compressor.CompressSpecificFilesInDir(srcDir, files, CompressorOptions{})
108132
Expect(err).ToNot(HaveOccurred())
109133
defer os.Remove(tgzName)
110134

@@ -182,7 +206,7 @@ var _ = Describe("tarballCompressor", func() {
182206
Expect(cmdRunner.RunCommands[0]).To(Equal(
183207
[]string{
184208
"tar", "--no-same-owner",
185-
"-xzf", tarballPath,
209+
"-xf", tarballPath,
186210
"-C", dstDir,
187211
},
188212
))
@@ -204,7 +228,7 @@ var _ = Describe("tarballCompressor", func() {
204228
Expect(cmdRunner.RunCommands[0]).To(Equal(
205229
[]string{
206230
"tar", "--same-owner",
207-
"-xzf", tarballPath,
231+
"-xf", tarballPath,
208232
"-C", dstDir,
209233
},
210234
))
@@ -222,7 +246,7 @@ var _ = Describe("tarballCompressor", func() {
222246
Expect(cmdRunner.RunCommands[0]).To(Equal(
223247
[]string{
224248
"tar", "--no-same-owner",
225-
"-xzf", tarballPath,
249+
"-xf", tarballPath,
226250
"-C", dstDir,
227251
"some/path/in/archive",
228252
},
@@ -241,7 +265,7 @@ var _ = Describe("tarballCompressor", func() {
241265
Expect(cmdRunner.RunCommands[0]).To(Equal(
242266
[]string{
243267
"tar", "--no-same-owner",
244-
"-xzf", tarballPath,
268+
"-xf", tarballPath,
245269
"-C", dstDir,
246270
"--strip-components=3",
247271
},

0 commit comments

Comments
 (0)