Skip to content

Commit

Permalink
Introduce new flags for resource-upload
Browse files Browse the repository at this point in the history
This change introduces the new flags --force and --resume-with-file.
It also fixes a bug which resulted in too many open files (#24).
  • Loading branch information
robertlemke committed Jan 25, 2021
1 parent 53d2331 commit 191465e
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions cmd/beach/cmd/resource-upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ import (
"path/filepath"
)

var instanceIdentifier, projectNamespace string
var instanceIdentifier, projectNamespace, resumeWithFile string
var force bool

// resourceUploadCmd represents the resource-upload command
var resourceUploadCmd = &cobra.Command{
Expand Down Expand Up @@ -56,6 +57,8 @@ Notes:
func init() {
resourceUploadCmd.Flags().StringVar(&instanceIdentifier, "instance", "", "instance identifier of the Beach instance to upload to, eg. 'instance-123abc45-def6-7890-abcd-1234567890ab'")
resourceUploadCmd.Flags().StringVar(&projectNamespace, "namespace", "", "The project namespace of the Beach instance to upload to, eg. 'project-123abc45-def6-7890-abcd-1234567890ab'")
resourceUploadCmd.Flags().BoolVar(&force, "force", false, "Force uploading resources which already exist in the target bucket")
resourceUploadCmd.Flags().StringVar(&resumeWithFile, "resume-with-file", "", "If specified, resume uploading resources starting with the given filename, eg. '12dcde4c13142942288c5a973caf0fa720ed2794'")
_ = resourceUploadCmd.MarkFlagRequired("instance")
_ = resourceUploadCmd.MarkFlagRequired("namespace")
rootCmd.AddCommand(resourceUploadCmd)
Expand Down Expand Up @@ -103,25 +106,42 @@ func handleResourceUploadRun(cmd *cobra.Command, args []string) {
bucket := client.Bucket(bucketName)
for _, pathAndFilename := range fileList {
filename := filepath.Base(pathAndFilename)
file, err := os.Open(pathAndFilename)
if err != nil {
log.Fatal(err)
return

if resumeWithFile != "" && filename < resumeWithFile {
log.Debug("Skipped " + filename)
continue
}
defer file.Close()

wc := bucket.Object(filename).NewWriter(ctx)
if _, err = io.Copy(wc, file); err != nil {
source, err := os.Open(pathAndFilename)
if err != nil {
log.Fatal(err)
return
}
if err := wc.Close(); err != nil {
log.Fatal(err)
return

_, err = bucket.Object(filename).Attrs(ctx)
if err == storage.ErrObjectNotExist || force == true {
destination := bucket.Object(filename).NewWriter(ctx)
if _, err = io.Copy(destination, source); err != nil {
log.Fatal(err)
return
}
if err := destination.Close(); err != nil {
log.Fatal(err)
return
}

if err = source.Close(); err != nil {
log.Error(err)
} else {
log.Debug("Uploaded " + filename)
}
} else if err == nil {
log.Debug("Skipped " + filename + " (already exists)")
} else {
log.Error(err)
}
log.Debug("Uploaded " + filename)
}

log.Info("Done")
return
}
}

0 comments on commit 191465e

Please sign in to comment.