From 428da4bcfedb3d3a2441d1d236f861e72ee8f5c6 Mon Sep 17 00:00:00 2001 From: Petter Rasmussen Date: Sun, 21 Feb 2016 12:55:09 +0100 Subject: Support downloading files by query --- drive/download.go | 41 +++++++++++++++++++++++++++++++++++++++++ gdrive.go | 33 +++++++++++++++++++++++++++++++++ handlers_drive.go | 13 +++++++++++++ 3 files changed, 87 insertions(+) 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 { diff --git a/gdrive.go b/gdrive.go index f89688b..ffcf30f 100644 --- a/gdrive.go +++ b/gdrive.go @@ -131,6 +131,39 @@ func main() { ), }, }, + &cli.Handler{ + Pattern: "[global] download query [options] ", + 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] ", Description: "Upload file or directory", 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) -- cgit v1.2.3