Skip to content

Commit

Permalink
Merge pull request #40 from flownative/bugfix/close-file-handles
Browse files Browse the repository at this point in the history
Make sure local file handles are closed on error
  • Loading branch information
kitsunet authored Mar 28, 2022
2 parents decd243 + b00d947 commit 4b511b8
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions cmd/beach/cmd/resource-upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand All @@ -144,4 +146,4 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) {

log.Info("Done")
return
}
}

0 comments on commit 4b511b8

Please sign in to comment.