From 228cda5531e57fa40b760269acfd9ca9b15a45ec Mon Sep 17 00:00:00 2001 From: Wayne Starr Date: Thu, 31 Aug 2023 17:05:44 -0500 Subject: [PATCH 1/3] Fix tmpdir behavior --- src/internal/packager/git/pull.go | 3 ++- src/internal/packager/helm/post-render.go | 2 +- src/pkg/oci/utils.go | 3 ++- src/pkg/packager/common.go | 13 ++++--------- src/pkg/packager/create.go | 2 +- src/pkg/utils/io.go | 9 +++++++-- 6 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/internal/packager/git/pull.go b/src/internal/packager/git/pull.go index fb40a27ad6..79313cde32 100644 --- a/src/internal/packager/git/pull.go +++ b/src/internal/packager/git/pull.go @@ -9,6 +9,7 @@ import ( "path" "strings" + "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/pkg/transform" "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/go-git/go-git/v5/plumbing" @@ -18,7 +19,7 @@ import ( func (g *Git) DownloadRepoToTemp(gitURL string) error { g.Spinner.Updatef("g.DownloadRepoToTemp(%s)", gitURL) - path, err := utils.MakeTempDir() + path, err := utils.MakeTempDir(config.CommonOptions.TempDirectory) if err != nil { return fmt.Errorf("unable to create tmpdir: %w", err) } diff --git a/src/internal/packager/helm/post-render.go b/src/internal/packager/helm/post-render.go index 31e1e866e8..3441e0a79e 100644 --- a/src/internal/packager/helm/post-render.go +++ b/src/internal/packager/helm/post-render.go @@ -55,7 +55,7 @@ func (h *Helm) newRenderer() (*renderer, error) { func (r *renderer) Run(renderedManifests *bytes.Buffer) (*bytes.Buffer, error) { // This is very low cost and consistent for how we replace elsewhere, also good for debugging - tempDir, err := utils.MakeTempDir() + tempDir, err := utils.MakeTempDir(config.CommonOptions.TempDirectory) if err != nil { return nil, fmt.Errorf("unable to create tmpdir: %w", err) } diff --git a/src/pkg/oci/utils.go b/src/pkg/oci/utils.go index 876725b15b..fc7f391ea1 100644 --- a/src/pkg/oci/utils.go +++ b/src/pkg/oci/utils.go @@ -12,6 +12,7 @@ import ( "path/filepath" "strings" + "github.com/defenseunicorns/zarf/src/config" "github.com/defenseunicorns/zarf/src/pkg/message" "github.com/defenseunicorns/zarf/src/pkg/utils" "github.com/defenseunicorns/zarf/src/pkg/utils/helpers" @@ -107,7 +108,7 @@ func DownloadPackageTarball(url, destinationTarball string, concurrency int) err return err } - tmp, err := utils.MakeTempDir() + tmp, err := utils.MakeTempDir(config.CommonOptions.TempDirectory) if err != nil { return err } diff --git a/src/pkg/packager/common.go b/src/pkg/packager/common.go index 4c31f34757..b3a748ded6 100644 --- a/src/pkg/packager/common.go +++ b/src/pkg/packager/common.go @@ -241,16 +241,11 @@ func isValidFileExtension(filename string) bool { } func createPaths(basePath string) (paths types.TempPaths, err error) { - if basePath == "" { - basePath, err = utils.MakeTempDir() - if err != nil { - return paths, err - } - } else { - if err := utils.CreateDirectory(basePath, 0700); err != nil { - return paths, fmt.Errorf("unable to create temp directory: %w", err) - } + basePath, err = utils.MakeTempDir(basePath) + if err != nil { + return paths, err } + message.Debug("Using temporary directory:", basePath) paths = types.TempPaths{ Base: basePath, diff --git a/src/pkg/packager/create.go b/src/pkg/packager/create.go index 62d7177347..4ecaf938b1 100755 --- a/src/pkg/packager/create.go +++ b/src/pkg/packager/create.go @@ -619,7 +619,7 @@ func (p *Packager) loadDifferentialData() error { // Save the fact that this is a differential build into the build data of the package p.cfg.Pkg.Build.Differential = true - tmpDir, _ := utils.MakeTempDir() + tmpDir, _ := utils.MakeTempDir(config.CommonOptions.TempDirectory) defer os.RemoveAll(tmpDir) // Load the package spec of the package we're using as a 'reference' for the differential build diff --git a/src/pkg/utils/io.go b/src/pkg/utils/io.go index 3c3ab99d25..1d579b8308 100755 --- a/src/pkg/utils/io.go +++ b/src/pkg/utils/io.go @@ -59,8 +59,13 @@ type TextTemplate struct { } // MakeTempDir creates a temp directory with the zarf- prefix. -func MakeTempDir() (string, error) { - tmp, err := os.MkdirTemp("", tmpPathPrefix) +func MakeTempDir(basePath string) (string, error) { + if InvalidPath(basePath) { + if err := CreateDirectory(basePath, 0700); err != nil { + return "", err + } + } + tmp, err := os.MkdirTemp(basePath, tmpPathPrefix) message.Debugf("Creating temp path: '%s'", tmp) return tmp, err } From cc5a52ac0d8e507c1ac49e1a45b8d1f9e97cc127 Mon Sep 17 00:00:00 2001 From: Wayne Starr Date: Thu, 31 Aug 2023 22:36:15 -0500 Subject: [PATCH 2/3] Fix empty basePath --- src/pkg/utils/io.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pkg/utils/io.go b/src/pkg/utils/io.go index 1d579b8308..086bf9590c 100755 --- a/src/pkg/utils/io.go +++ b/src/pkg/utils/io.go @@ -60,7 +60,7 @@ type TextTemplate struct { // MakeTempDir creates a temp directory with the zarf- prefix. func MakeTempDir(basePath string) (string, error) { - if InvalidPath(basePath) { + if basePath != "" && InvalidPath(basePath) { if err := CreateDirectory(basePath, 0700); err != nil { return "", err } From 2dfe04a5b1cba2905757c2b165bdf0cf5f12a868 Mon Sep 17 00:00:00 2001 From: Wayne Starr Date: Sat, 2 Sep 2023 01:56:05 -0500 Subject: [PATCH 3/3] Resolve feedback --- src/pkg/packager/common.go | 1 - src/pkg/utils/io.go | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/pkg/packager/common.go b/src/pkg/packager/common.go index b3a748ded6..dd801bd8f1 100644 --- a/src/pkg/packager/common.go +++ b/src/pkg/packager/common.go @@ -246,7 +246,6 @@ func createPaths(basePath string) (paths types.TempPaths, err error) { return paths, err } - message.Debug("Using temporary directory:", basePath) paths = types.TempPaths{ Base: basePath, diff --git a/src/pkg/utils/io.go b/src/pkg/utils/io.go index 086bf9590c..70fcb7323c 100755 --- a/src/pkg/utils/io.go +++ b/src/pkg/utils/io.go @@ -60,13 +60,13 @@ type TextTemplate struct { // MakeTempDir creates a temp directory with the zarf- prefix. func MakeTempDir(basePath string) (string, error) { - if basePath != "" && InvalidPath(basePath) { + if basePath != "" { if err := CreateDirectory(basePath, 0700); err != nil { return "", err } } tmp, err := os.MkdirTemp(basePath, tmpPathPrefix) - message.Debugf("Creating temp path: '%s'", tmp) + message.Debug("Using temporary directory:", basePath) return tmp, err }