Skip to content

Commit

Permalink
Add dry-run flag for syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 3, 2016
1 parent 6d0a23e commit 5f1972a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
21 changes: 21 additions & 0 deletions drive/sync_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type DownloadSyncArgs struct {
Progress io.Writer
RootId string
Path string
DryRun bool
DeleteExtraneous bool
}

Expand Down Expand Up @@ -104,6 +105,11 @@ func (self *Drive) createMissingLocalDirs(files *syncFiles, args DownloadSyncArg
return nil, fmt.Errorf("Failed to determine local absolute path: %s", err)
}
fmt.Fprintf(args.Out, "[%04d/%04d] Creating directory: %s\n", i + 1, missingCount, path)

if args.DryRun {
continue
}

mkdir(path)
}

Expand All @@ -125,6 +131,11 @@ func (self *Drive) downloadMissingFiles(files *syncFiles, args DownloadSyncArgs)
return fmt.Errorf("Failed to determine local absolute path: %s", err)
}
fmt.Fprintf(args.Out, "[%04d/%04d] Downloading %s -> %s\n", i + 1, missingCount, remotePath, localPath)

if args.DryRun {
continue
}

err = self.downloadRemoteFile(rf.file.Id, localPath, args)
if err != nil {
return err
Expand All @@ -149,6 +160,11 @@ func (self *Drive) downloadChangedFiles(files *syncFiles, args DownloadSyncArgs)
return fmt.Errorf("Failed to determine local absolute path: %s", err)
}
fmt.Fprintf(args.Out, "[%04d/%04d] Downloading %s -> %s\n", i + 1, changedCount, remotePath, localPath)

if args.DryRun {
continue
}

err = self.downloadRemoteFile(cf.remote.file.Id, localPath, args)
if err != nil {
return err
Expand Down Expand Up @@ -206,6 +222,11 @@ func (self *Drive) deleteExtraneousLocalFiles(files *syncFiles, args DownloadSyn

for i, lf := range extraneousFiles {
fmt.Fprintf(args.Out, "[%04d/%04d] Deleting %s\n", i + 1, extraneousCount, lf.absPath)

if args.DryRun {
continue
}

err := os.Remove(lf.absPath)
if err != nil {
return fmt.Errorf("Failed to delete local file: %s", err)
Expand Down
39 changes: 31 additions & 8 deletions drive/sync_upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type UploadSyncArgs struct {
Progress io.Writer
Path string
RootId string
DryRun bool
DeleteExtraneous bool
ChunkSize int64
}
Expand Down Expand Up @@ -141,15 +142,22 @@ func (self *Drive) createMissingRemoteDirs(files *syncFiles, args UploadSyncArgs

fmt.Fprintf(args.Out, "[%04d/%04d] Creating directory: %s\n", i + 1, missingCount, filepath.Join(files.root.file.Name, lf.relPath))

f, err := self.service.Files.Create(dstFile).Do()
if err != nil {
return nil, fmt.Errorf("Failed to create directory: %s", err)
if args.DryRun {
files.remote = append(files.remote, &remoteFile{
relPath: lf.relPath,
file: dstFile,
})
} else {
f, err := self.service.Files.Create(dstFile).Do()
if err != nil {
return nil, fmt.Errorf("Failed to create directory: %s", err)
}

files.remote = append(files.remote, &remoteFile{
relPath: lf.relPath,
file: f,
})
}

files.remote = append(files.remote, &remoteFile{
relPath: lf.relPath,
file: f,
})
}

return files, nil
Expand All @@ -171,6 +179,11 @@ func (self *Drive) uploadMissingFiles(files *syncFiles, args UploadSyncArgs) err
}

fmt.Fprintf(args.Out, "[%04d/%04d] Uploading %s -> %s\n", i + 1, missingCount, lf.absPath, filepath.Join(files.root.file.Name, lf.relPath))

if args.DryRun {
continue
}

err := self.uploadMissingFile(parent.file.Id, lf, args)
if err != nil {
return err
Expand All @@ -190,6 +203,11 @@ func (self *Drive) updateChangedFiles(files *syncFiles, args UploadSyncArgs) err

for i, cf := range changedFiles {
fmt.Fprintf(args.Out, "[%04d/%04d] Updating %s -> %s\n", i + 1, changedCount, cf.local.absPath, filepath.Join(files.root.file.Name, cf.local.relPath))

if args.DryRun {
continue
}

err := self.updateChangedFile(cf, args)
if err != nil {
return err
Expand All @@ -212,6 +230,11 @@ func (self *Drive) deleteExtraneousRemoteFiles(files *syncFiles, args UploadSync

for i, rf := range extraneousFiles {
fmt.Fprintf(args.Out, "[%04d/%04d] Deleting %s\n", i + 1, extraneousCount, filepath.Join(files.root.file.Name, rf.relPath))

if args.DryRun {
continue
}

err := self.deleteRemoteFile(rf, args)
if err != nil {
return err
Expand Down
12 changes: 12 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,12 @@ func main() {
Description: "Hide progress",
OmitValue: true,
},
cli.BoolFlag{
Name: "dryRun",
Patterns: []string{"--dry-run"},
Description: "Show what would have been transferred",
OmitValue: true,
},
cli.BoolFlag{
Name: "deleteExtraneous",
Patterns: []string{"--delete-extraneous"},
Expand All @@ -357,6 +363,12 @@ func main() {
Flags: cli.Flags{
"global": globalFlags,
"options": []cli.Flag{
cli.BoolFlag{
Name: "dryRun",
Patterns: []string{"--dry-run"},
Description: "Show what would have been transferred",
OmitValue: true,
},
cli.BoolFlag{
Name: "noProgress",
Patterns: []string{"--no-progress"},
Expand Down
2 changes: 2 additions & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func downloadSyncHandler(ctx cli.Context) {
Progress: progressWriter(args.Bool("noProgress")),
Path: args.String("path"),
RootId: args.String("id"),
DryRun: args.Bool("dryRun"),
DeleteExtraneous: args.Bool("deleteExtraneous"),
})
checkErr(err)
Expand Down Expand Up @@ -118,6 +119,7 @@ func uploadSyncHandler(ctx cli.Context) {
Progress: progressWriter(args.Bool("noProgress")),
Path: args.String("path"),
RootId: args.String("id"),
DryRun: args.Bool("dryRun"),
DeleteExtraneous: args.Bool("deleteExtraneous"),
ChunkSize: args.Int64("chunksize"),
})
Expand Down

0 comments on commit 5f1972a

Please sign in to comment.