Improve error handling and close files post-tarring

This commit is contained in:
Kegan Dougal 2021-07-08 10:07:39 +01:00
parent f2974721d5
commit 717d16345c
2 changed files with 8 additions and 1 deletions

View file

@ -104,6 +104,7 @@ func downloadArchive(cli *http.Client, tmpDir, archiveURL string, dockerfile []b
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, fmt.Errorf("got HTTP %d", resp.StatusCode) return nil, fmt.Errorf("got HTTP %d", resp.StatusCode)
} }
_ = os.RemoveAll(tmpDir)
if err = os.Mkdir(tmpDir, os.ModePerm); err != nil { if err = os.Mkdir(tmpDir, os.ModePerm); err != nil {
return nil, fmt.Errorf("failed to make temporary directory: %s", err) return nil, fmt.Errorf("failed to make temporary directory: %s", err)
} }

View file

@ -17,7 +17,7 @@ func compress(src string, buf io.Writer) error {
tw := tar.NewWriter(zr) tw := tar.NewWriter(zr)
// walk through every file in the folder // walk through every file in the folder
_ = filepath.Walk(src, func(file string, fi os.FileInfo, e error) error { err := filepath.Walk(src, func(file string, fi os.FileInfo, e error) error {
// generate tar header // generate tar header
header, err := tar.FileInfoHeader(fi, file) header, err := tar.FileInfoHeader(fi, file)
if err != nil { if err != nil {
@ -40,9 +40,15 @@ func compress(src string, buf io.Writer) error {
if _, err := io.Copy(tw, data); err != nil { if _, err := io.Copy(tw, data); err != nil {
return err return err
} }
if err = data.Close(); err != nil {
return err
}
} }
return nil return nil
}) })
if err != nil {
return err
}
// produce tar // produce tar
if err := tw.Close(); err != nil { if err := tw.Close(); err != nil {