From b00d947b015d68c53af893ddcde1c2b8cac2a54f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Mu=CC=88ller?= Date: Mon, 28 Mar 2022 18:59:30 +0200 Subject: [PATCH] Make sure local file handles are closed on error --- cmd/beach/cmd/resource-upload.go | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/cmd/beach/cmd/resource-upload.go b/cmd/beach/cmd/resource-upload.go index dfc412b..f735ba6 100644 --- a/cmd/beach/cmd/resource-upload.go +++ b/cmd/beach/cmd/resource-upload.go @@ -16,16 +16,17 @@ package cmd import ( - "cloud.google.com/go/storage" "context" "fmt" + "io" + "os" + "path/filepath" + + "cloud.google.com/go/storage" "github.com/flownative/localbeach/pkg/beachsandbox" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "google.golang.org/api/option" - "io" - "os" - "path/filepath" ) var instanceIdentifier, projectNamespace, resumeWithFile string @@ -50,8 +51,8 @@ Notes: - existing data in the Beach instance will be left unchanged - older instances may use a namespace called "beach" `, - Args: cobra.ExactArgs(0), - Run: handleResourceUploadRun, + Args: cobra.ExactArgs(0), + Run: handleResourceUploadRun, } func init() { @@ -112,20 +113,21 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) { continue } - source, err := os.Open(pathAndFilename) - if err != nil { - log.Fatal(err) - return - } - _, err = bucket.Object(filename).Attrs(ctx) if err == storage.ErrObjectNotExist || force == true { + source, err := os.Open(pathAndFilename) + if err != nil { + log.Fatal(err) + return + } destination := bucket.Object(filename).NewWriter(ctx) if _, err = io.Copy(destination, source); err != nil { + source.Close() log.Fatal(err) return } if err := destination.Close(); err != nil { + source.Close() log.Fatal(err) return } @@ -144,4 +146,4 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) { log.Info("Done") return -} \ No newline at end of file +}