Skip to content

Commit

Permalink
fix(create/init): Download into a temporary directory rather than the…
Browse files Browse the repository at this point in the history
… current directory.
  • Loading branch information
harmony7 committed Nov 18, 2024
1 parent 5c136b6 commit 51c590e
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions pkg/commands/compute/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,16 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
return err
}

filename := filepath.Base(c.CloneFrom)
tempdir, err := tempDir("package-init-download")
if err != nil {
return fmt.Errorf("error creating temporary path for package template: %w", err)
}
defer os.RemoveAll(tempdir)

filename := filepath.Join(
tempdir,
filepath.Base(c.CloneFrom),
)
ext := filepath.Ext(filename)

// gosec flagged this:
Expand All @@ -963,16 +972,6 @@ func (c *InitCommand) FetchPackageTemplate(branch, tag string, archives []file.A
}
return err
}
defer func() {
// NOTE: Later on we rename the file to include an extension and the
// following call to os.Remove works still because the `filename` variable
// that is still in scope is also updated to include the extension.
err := os.Remove(filename)
if err != nil {
c.Globals.ErrLog.Add(err)
text.Info(out, "We were unable to clean-up the local %s file (it can be safely removed)", filename)
}
}()

_, err = io.Copy(f, res.Body)
if err != nil {
Expand Down

0 comments on commit 51c590e

Please sign in to comment.