Skip to content

Commit

Permalink
add StripComponents and PathInArchive options to compressor
Browse files Browse the repository at this point in the history
StripComponents(--strip-components) strips segments of the path before
extracting. PathInArchive allows you to selectively chose what to
extract from the archive.

These are use in the bosh-agent to extract job bundles, since they have
more than one job per tarball.

[#161569370](https://www.pivotaltracker.com/story/show/161569370)

Co-authored-by: Christopher Brown <cbrown@pivotal.io>
  • Loading branch information
luan and Christopher Brown committed Nov 28, 2018
1 parent 7f31da9 commit ef74513
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion fileutil/compressor_interface.go
@@ -1,7 +1,9 @@
package fileutil

type CompressorOptions struct {
SameOwner bool
SameOwner bool
PathInArchive string
StripComponents int
}

type Compressor interface {
Expand Down
12 changes: 11 additions & 1 deletion fileutil/tarball_compressor.go
@@ -1,6 +1,8 @@
package fileutil

import (
"fmt"

bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
)
Expand Down Expand Up @@ -51,7 +53,15 @@ func (c tarballCompressor) DecompressFileToDir(tarballPath string, dir string, o
sameOwnerOption = "--same-owner"
}

_, _, _, err := c.cmdRunner.RunCommand("tar", sameOwnerOption, "-xzf", tarballPath, "-C", dir)
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")
}
Expand Down
38 changes: 38 additions & 0 deletions fileutil/tarball_compressor_test.go
Expand Up @@ -263,6 +263,44 @@ var _ = Describe("tarballCompressor", func() {
},
))
})

It("uses PathInArchive to select files from archive", func() {
cmdRunner := fakesys.NewFakeCmdRunner()
compressor := NewTarballCompressor(cmdRunner, fs)

tarballPath := fixtureSrcTgz()
err := compressor.DecompressFileToDir(tarballPath, dstDir, CompressorOptions{PathInArchive: "some/path/in/archive"})
Expect(err).ToNot(HaveOccurred())

Expect(1).To(Equal(len(cmdRunner.RunCommands)))
Expect(cmdRunner.RunCommands[0]).To(Equal(
[]string{
"tar", "--no-same-owner",
"-xzf", tarballPath,
"-C", dstDir,
"some/path/in/archive",
},
))
})

It("uses StripComponents option", func() {
cmdRunner := fakesys.NewFakeCmdRunner()
compressor := NewTarballCompressor(cmdRunner, fs)

tarballPath := fixtureSrcTgz()
err := compressor.DecompressFileToDir(tarballPath, dstDir, CompressorOptions{StripComponents: 3})
Expect(err).ToNot(HaveOccurred())

Expect(1).To(Equal(len(cmdRunner.RunCommands)))
Expect(cmdRunner.RunCommands[0]).To(Equal(
[]string{
"tar", "--no-same-owner",
"-xzf", tarballPath,
"-C", dstDir,
"--strip-components=3",
},
))
})
})

Describe("CleanUp", func() {
Expand Down

0 comments on commit ef74513

Please sign in to comment.