diff --git a/internal/mirror/cmd/pull/pull.go b/internal/mirror/cmd/pull/pull.go index a0706176..7357c94b 100644 --- a/internal/mirror/cmd/pull/pull.go +++ b/internal/mirror/cmd/pull/pull.go @@ -171,6 +171,7 @@ func buildPullParams(logger params.Logger) *params.PullParams { BundleDir: ImagesBundlePath, WorkingDir: filepath.Join( TempDir, + "mirror", "pull", fmt.Sprintf("%x", md5.Sum([]byte(SourceRegistryRepo))), ), @@ -246,6 +247,9 @@ func NewPuller(cmd *cobra.Command) *Puller { } } func (p *Puller) Execute() error { + // Ensure temporary directory is cleaned up even if an error occurs + defer p.finalCleanup() + if err := p.cleanupWorkingDirectory(); err != nil { return err } @@ -266,7 +270,7 @@ func (p *Puller) Execute() error { return err } - return p.finalCleanup() + return nil } // cleanupWorkingDirectory handles cleanup of the working directory if needed diff --git a/internal/mirror/cmd/pull/pull_test.go b/internal/mirror/cmd/pull/pull_test.go index 6c60336f..511ac81f 100644 --- a/internal/mirror/cmd/pull/pull_test.go +++ b/internal/mirror/cmd/pull/pull_test.go @@ -206,6 +206,7 @@ func TestBuildPullParams(t *testing.T) { // Check working directory calculation expectedWorkingDir := filepath.Join( TempDir, + "mirror", "pull", fmt.Sprintf("%x", md5.Sum([]byte(SourceRegistryRepo))), ) @@ -422,7 +423,7 @@ func TestWorkingDirectoryCalculation(t *testing.T) { params := buildPullParams(logger) expectedHash := fmt.Sprintf("%x", md5.Sum([]byte(SourceRegistryRepo))) - expectedPath := filepath.Join(TempDir, "pull", expectedHash) + expectedPath := filepath.Join(TempDir, "mirror", "pull", expectedHash) assert.Equal(t, expectedPath, params.WorkingDir) assert.Contains(t, params.WorkingDir, "pull") @@ -528,7 +529,7 @@ func TestValidateTmpPathEmpty(t *testing.T) { assert.NoError(t, err) // Check that TempDir was set to default - expectedTempDir := filepath.Join(tempDir, ".tmp", "mirror") + expectedTempDir := filepath.Join(tempDir, ".tmp") assert.Equal(t, expectedTempDir, TempDir) // Check that directory was created diff --git a/internal/mirror/cmd/pull/validation.go b/internal/mirror/cmd/pull/validation.go index 70609e60..166b36fe 100644 --- a/internal/mirror/cmd/pull/validation.go +++ b/internal/mirror/cmd/pull/validation.go @@ -137,7 +137,7 @@ func validateChunkSizeFlag() error { func validateTmpPath(args []string) error { if TempDir == "" { - TempDir = filepath.Join(ImagesBundlePath, ".tmp", "mirror") + TempDir = filepath.Join(ImagesBundlePath, ".tmp") } if err := os.MkdirAll(TempDir, 0755); err != nil { return fmt.Errorf("Error creating temp directory at %s: %w", TempDir, err) diff --git a/internal/mirror/cmd/push/push.go b/internal/mirror/cmd/push/push.go index 0ef51a35..57a9def1 100644 --- a/internal/mirror/cmd/push/push.go +++ b/internal/mirror/cmd/push/push.go @@ -205,7 +205,7 @@ func buildPushParams(logger params.Logger) *params.PushParams { RegistryPath: RegistryPath, ModulesPathSuffix: ModulesPathSuffix, BundleDir: ImagesBundlePath, - WorkingDir: filepath.Join(TempDir, "push"), + WorkingDir: filepath.Join(TempDir, "mirror", "push"), }, Parallelism: params.ParallelismConfig{ diff --git a/internal/mirror/cmd/push/validation.go b/internal/mirror/cmd/push/validation.go index 4235b4e1..9af5709e 100644 --- a/internal/mirror/cmd/push/validation.go +++ b/internal/mirror/cmd/push/validation.go @@ -69,7 +69,7 @@ func validateImagesBundlePathArg(args []string) error { } if TempDir == "" { - TempDir = filepath.Join(ImagesBundlePath, ".tmp", "mirror") + TempDir = filepath.Join(ImagesBundlePath, ".tmp") } return nil @@ -77,7 +77,7 @@ func validateImagesBundlePathArg(args []string) error { if bundleExtension := filepath.Ext(ImagesBundlePath); bundleExtension == ".tar" || bundleExtension == ".chunk" { if TempDir == "" { - TempDir = filepath.Join(filepath.Dir(ImagesBundlePath), ".tmp", "mirror") + TempDir = filepath.Join(filepath.Dir(ImagesBundlePath), ".tmp") } return nil }