builder/file: defer close after testing for errors

This commit is contained in:
Lars Lehtonen 2019-11-05 11:40:42 -08:00
parent d7b2aa88c4
commit 5a0dc48f0f
No known key found for this signature in database
GPG Key ID: 8137D474EBCB04F2
1 changed files with 2 additions and 2 deletions

View File

@ -39,17 +39,17 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack
if b.config.Source != "" {
source, err := os.Open(b.config.Source)
defer source.Close()
if err != nil {
return nil, err
}
defer source.Close()
// Create will truncate an existing file
target, err := os.Create(b.config.Target)
defer target.Close()
if err != nil {
return nil, err
}
defer target.Close()
ui.Say(fmt.Sprintf("Copying %s to %s", source.Name(), target.Name()))
bytes, err := io.Copy(target, source)