diff options
| author | Petter Rasmussen | 2016-02-21 12:55:09 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-02-21 12:55:09 +0100 |
| commit | 428da4bcfedb3d3a2441d1d236f861e72ee8f5c6 (patch) | |
| tree | 733a538b24411d18490783d1e1c1d6ff8c6411b0 | |
| parent | 453384107e41991a5417c7c3ed78da6cb1c4a805 (diff) | |
| download | gdrive-428da4bcfedb3d3a2441d1d236f861e72ee8f5c6.tar.bz2 | |
Support downloading files by query
| -rw-r--r-- | drive/download.go | 41 | ||||
| -rw-r--r-- | gdrive.go | 33 | ||||
| -rw-r--r-- | handlers_drive.go | 13 |
3 files changed, 87 insertions, 0 deletions
diff --git a/drive/download.go b/drive/download.go index a33373f..1779d57 100644 --- a/drive/download.go +++ b/drive/download.go @@ -58,6 +58,47 @@ func (self *Drive) Download(args DownloadArgs) error { return err } +type DownloadQueryArgs struct { + Out io.Writer + Progress io.Writer + Query string + Path string + Force bool + Recursive bool +} + +func (self *Drive) DownloadQuery(args DownloadQueryArgs) error { + listArgs := listAllFilesArgs{ + query: args.Query, + fields: []googleapi.Field{"nextPageToken", "files(id,name,mimeType,size,md5Checksum)"}, + } + files, err := self.listAllFiles(listArgs) + if err != nil { + return fmt.Errorf("Failed to list files: %s", err) + } + + downloadArgs := DownloadArgs{ + Out: args.Out, + Progress: args.Progress, + Path: args.Path, + Force: args.Force, + } + + for _, f := range files { + if isDir(f) && args.Recursive { + err = self.downloadDirectory(f, downloadArgs) + } else if isBinary(f) { + _, _, err = self.downloadBinary(f, downloadArgs) + } + + if err != nil { + return err + } + } + + return nil +} + func (self *Drive) downloadRecursive(args DownloadArgs) error { f, err := self.service.Files.Get(args.Id).Fields("id", "name", "size", "mimeType", "md5Checksum").Do() if err != nil { @@ -132,6 +132,39 @@ func main() { }, }, &cli.Handler{ + Pattern: "[global] download query [options] <query>", + Description: "Download all files and directories matching query", + Callback: downloadQueryHandler, + FlagGroups: cli.FlagGroups{ + cli.NewFlagGroup("global", globalFlags...), + cli.NewFlagGroup("options", + cli.BoolFlag{ + Name: "force", + Patterns: []string{"-f", "--force"}, + Description: "Overwrite existing file", + OmitValue: true, + }, + cli.BoolFlag{ + Name: "recursive", + Patterns: []string{"-r", "--recursive"}, + Description: "Download directories recursively, documents will be skipped", + OmitValue: true, + }, + cli.StringFlag{ + Name: "path", + Patterns: []string{"--path"}, + Description: "Download path", + }, + cli.BoolFlag{ + Name: "noProgress", + Patterns: []string{"--no-progress"}, + Description: "Hide progress", + OmitValue: true, + }, + ), + }, + }, + &cli.Handler{ Pattern: "[global] upload [options] <path>", Description: "Upload file or directory", Callback: uploadHandler, diff --git a/handlers_drive.go b/handlers_drive.go index 957161f..3698d0e 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -61,6 +61,19 @@ func downloadHandler(ctx cli.Context) { checkErr(err) } +func downloadQueryHandler(ctx cli.Context) { + args := ctx.Args() + err := newDrive(args).DownloadQuery(drive.DownloadQueryArgs{ + Out: os.Stdout, + Query: args.String("query"), + Force: args.Bool("force"), + Recursive: args.Bool("recursive"), + Path: args.String("path"), + Progress: progressWriter(args.Bool("noProgress")), + }) + checkErr(err) +} + func downloadSyncHandler(ctx cli.Context) { args := ctx.Args() cachePath := filepath.Join(args.String("configDir"), DefaultCacheFileName) |
