From 1afee5b660f7cabc994f5f90bb8619677f9a94fa Mon Sep 17 00:00:00 2001 From: Jeremy Beard Date: Thu, 25 Jul 2024 10:50:22 -0400 Subject: [PATCH] Only exclude .git for bundles, not all hidden paths (#1686) --- cloud/deploy/bundle.go | 2 +- pkg/fileutil/files.go | 21 ++++++++++++++------- pkg/fileutil/files_test.go | 25 ++++++++++++++++++------- software/deploy/deploy.go | 2 +- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/cloud/deploy/bundle.go b/cloud/deploy/bundle.go index 8c25d3e87..a3066b742 100644 --- a/cloud/deploy/bundle.go +++ b/cloud/deploy/bundle.go @@ -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 } diff --git a/pkg/fileutil/files.go b/pkg/fileutil/files.go index 38960dcb2..e2875fa20 100644 --- a/pkg/fileutil/files.go +++ b/pkg/fileutil/files.go @@ -18,6 +18,7 @@ import ( "github.com/astronomer/astro-cli/pkg/util" "github.com/pkg/errors" + "github.com/sirupsen/logrus" "github.com/spf13/afero" ) @@ -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 @@ -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 } @@ -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 diff --git a/pkg/fileutil/files_test.go b/pkg/fileutil/files_test.go index 41002e928..0908bf809 100644 --- a/pkg/fileutil/files_test.go +++ b/pkg/fileutil/files_test.go @@ -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" @@ -183,6 +180,7 @@ func (s *Suite) TestTar() { source string target string prependBaseDir bool + excludePaths []string } tests := []struct { name string @@ -202,7 +200,6 @@ func (s *Suite) TestTar() { testFileName, symlinkFileName, filepath.Join(testSubDirName, testSubDirFileName), - // no hidden file }, }, { @@ -217,7 +214,21 @@ 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 }, }, } @@ -225,7 +236,7 @@ func (s *Suite) TestTar() { 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) diff --git a/software/deploy/deploy.go b/software/deploy/deploy.go index 02ecfe18d..eab6a7cec 100644 --- a/software/deploy/deploy.go +++ b/software/deploy/deploy.go @@ -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 }