Skip to content

Commit

Permalink
Only exclude .git for bundles, not all hidden paths (#1686)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybeard authored and kushalmalani committed Jul 25, 2024
1 parent cbbeb97 commit 1afee5b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cloud/deploy/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func uploadBundle(tarDirPath, bundlePath, uploadURL string, prependBaseDir bool)
}()

// Generate the bundle tar
err := fileutil.Tar(bundlePath, tarFilePath, prependBaseDir)
err := fileutil.Tar(bundlePath, tarFilePath, prependBaseDir, []string{".git/"})
if err != nil {
return "", err
}
Expand Down
21 changes: 14 additions & 7 deletions pkg/fileutil/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/astronomer/astro-cli/pkg/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)

Expand Down Expand Up @@ -95,7 +96,7 @@ func WriteToFile(path string, r io.Reader) error {
return err
}

func Tar(source, target string, prependBaseDir bool) error {
func Tar(source, target string, prependBaseDir bool, excludePathPrefixes []string) error {
tarfile, err := os.Create(target)
if err != nil {
return err
Expand Down Expand Up @@ -124,11 +125,6 @@ func Tar(source, target string, prependBaseDir bool) error {
return nil
}

// ignore hidden files, and files in hidden directories
if IsHidden(path) {
return nil
}

if path == target {
return nil
}
Expand All @@ -154,7 +150,18 @@ func Tar(source, target string, prependBaseDir bool) error {
headerName = filepath.Join(baseDir, headerName)
}
// force use forward slashes in tar files
header.Name = filepath.ToSlash(headerName)
headerName = filepath.ToSlash(headerName)

// ignore excluded paths
for _, excludePathPrefix := range excludePathPrefixes {
if strings.HasPrefix(headerName, excludePathPrefix) {
logrus.Debugf("Excluding tarball path: %s", headerName)
return nil
}
}

header.Name = headerName
logrus.Debugf("Adding to tarball: %s", header.Name)

if err := tarball.WriteHeader(header); err != nil {
return err
Expand Down
25 changes: 18 additions & 7 deletions pkg/fileutil/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,13 @@ func (s *Suite) TestTar() {
testSourceDirPath := filepath.Join(testDirPath, testSourceDirName)
defer afero.NewOsFs().Remove(testSourceDirPath)

// create a test file, and a symlink to it, and a hidden test file
// create a test file, and a symlink to it
testFileName := "test.txt"
testFilePath := filepath.Join(testSourceDirPath, testFileName)
WriteStringToFile(testFilePath, "testing")
symlinkFileName := "symlink"
symlinkFilePath := filepath.Join(testSourceDirPath, symlinkFileName)
os.Symlink(testFilePath, symlinkFilePath)
hiddenTestFileName := ".hidden_test.txt"
hiddenTestFilePath := filepath.Join(testSourceDirPath, hiddenTestFileName)
WriteStringToFile(hiddenTestFilePath, "testing")

// create test file in a sub-directory
testSubDirFileName := "test_subdir.txt"
Expand All @@ -183,6 +180,7 @@ func (s *Suite) TestTar() {
source string
target string
prependBaseDir bool
excludePaths []string
}
tests := []struct {
name string
Expand All @@ -202,7 +200,6 @@ func (s *Suite) TestTar() {
testFileName,
symlinkFileName,
filepath.Join(testSubDirName, testSubDirFileName),
// no hidden file
},
},
{
Expand All @@ -217,15 +214,29 @@ func (s *Suite) TestTar() {
filepath.Join(testSourceDirName, testFileName),
filepath.Join(testSourceDirName, symlinkFileName),
filepath.Join(testSourceDirName, testSubDirName, testSubDirFileName),
// no hidden file
},
},
{
name: "exclude paths",
args: args{
source: testSourceDirPath,
target: filepath.Join(testDirPath, "test_exclude.tar"),
prependBaseDir: false,
excludePaths: []string{testSubDirName},
},
errAssertion: assert.NoError,
expectPaths: []string{
testFileName,
symlinkFileName,
// testSubDirFileName excluded
},
},
}

for _, tt := range tests {
s.Run(tt.name, func() {
// check that the tar operation was successful
assert.True(s.T(), tt.errAssertion(s.T(), Tar(tt.args.source, tt.args.target, tt.args.prependBaseDir)))
assert.True(s.T(), tt.errAssertion(s.T(), Tar(tt.args.source, tt.args.target, tt.args.prependBaseDir, tt.args.excludePaths)))

// check that all the files are in the tar at the correct paths
file, err := os.Open(tt.args.target)
Expand Down
2 changes: 1 addition & 1 deletion software/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ func DagsOnlyDeploy(houstonClient houston.ClientInterface, appConfig *houston.Ap
}

// Generate the dags tar
err = fileutil.Tar(dagsPath, dagsTarPath, true)
err = fileutil.Tar(dagsPath, dagsTarPath, true, nil)
if err != nil {
return err
}
Expand Down

0 comments on commit 1afee5b

Please sign in to comment.