From ad84abf41588eeab78484caff876546ecf2d4199 Mon Sep 17 00:00:00 2001 From: shreyas-goenka <88374338+shreyas-goenka@users.noreply.github.com> Date: Mon, 11 Sep 2023 12:22:05 +0200 Subject: [PATCH] Fix temporary directory cleanup for init repository downloading (#760) ## Changes This PR fixes a bug where the temp directory created to download the template would not be cleaned up. ## Tests Tested manually. The exact process is described in a comment below. --- cmd/bundle/init.go | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cmd/bundle/init.go b/cmd/bundle/init.go index 9a11eb25..3038cb7a 100644 --- a/cmd/bundle/init.go +++ b/cmd/bundle/init.go @@ -74,22 +74,21 @@ func newInitCommand() *cobra.Command { return template.Materialize(ctx, configFile, templatePath, outputDir) } - // Download the template in a temporary directory - tmpDir := os.TempDir() - templateURL := templatePath - repoDir := filepath.Join(tmpDir, repoName(templateURL)) - err := os.MkdirAll(repoDir, 0755) + // Create a temporary directory with the name of the repository. The '*' + // character is replaced by a random string in the generated temporary directory. + repoDir, err := os.MkdirTemp("", repoName(templatePath)+"-*") if err != nil { return err } // TODO: Add automated test that the downloaded git repo is cleaned up. - err = git.Clone(ctx, templateURL, "", repoDir) + // Clone the repository in the temporary directory + err = git.Clone(ctx, templatePath, "", repoDir) if err != nil { return err } - defer os.RemoveAll(templateDir) + // Clean up downloaded repository once the template is materialized. + defer os.RemoveAll(repoDir) return template.Materialize(ctx, configFile, filepath.Join(repoDir, templateDir), outputDir) } - return cmd }