diff options
| author | Petter Rasmussen | 2016-02-03 20:52:51 +0100 | 
|---|---|---|
| committer | Petter Rasmussen | 2016-02-03 20:52:51 +0100 | 
| commit | 5f1972a0d689b9965fc3fb9f5b9a5f9487fb0f46 (patch) | |
| tree | 562be8bd3843de3c9bdeb5099c9ec62211ec55d1 | |
| parent | 6d0a23e4bdf5ffa36cecb2cb85a6291f9235f619 (diff) | |
| download | gdrive-5f1972a0d689b9965fc3fb9f5b9a5f9487fb0f46.tar.bz2 | |
Add dry-run flag for syncing
| -rw-r--r-- | drive/sync_download.go | 21 | ||||
| -rw-r--r-- | drive/sync_upload.go | 39 | ||||
| -rw-r--r-- | gdrive.go | 12 | ||||
| -rw-r--r-- | handlers_drive.go | 2 | 
4 files changed, 66 insertions, 8 deletions
| diff --git a/drive/sync_download.go b/drive/sync_download.go index 37c78cb..ba86aae 100644 --- a/drive/sync_download.go +++ b/drive/sync_download.go @@ -16,6 +16,7 @@ type DownloadSyncArgs struct {      Progress io.Writer      RootId string      Path string +    DryRun bool      DeleteExtraneous bool  } @@ -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)      } @@ -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 @@ -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 @@ -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) diff --git a/drive/sync_upload.go b/drive/sync_upload.go index d1c155d..151a9f8 100644 --- a/drive/sync_upload.go +++ b/drive/sync_upload.go @@ -16,6 +16,7 @@ type UploadSyncArgs struct {      Progress io.Writer      Path string      RootId string +    DryRun bool      DeleteExtraneous bool      ChunkSize int64  } @@ -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 @@ -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 @@ -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 @@ -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 @@ -342,6 +342,12 @@ func main() {                          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"},                          Description: "Delete extraneous local files", @@ -358,6 +364,12 @@ func main() {                  "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"},                          Description: "Hide progress", diff --git a/handlers_drive.go b/handlers_drive.go index 2b6d61e..8e7540c 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -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) @@ -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"),      }) | 
