Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Dont download data if file is skipped #347

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 0 additions & 48 deletions Godeps/Godeps.json

This file was deleted.

5 changes: 0 additions & 5 deletions Godeps/Readme

This file was deleted.

13 changes: 13 additions & 0 deletions drive/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type AboutArgs struct {
Out io.Writer
SizeInBytes bool
JsonOutput int64
}

func (self *Drive) About(args AboutArgs) (err error) {
Expand All @@ -20,6 +21,18 @@ func (self *Drive) About(args AboutArgs) (err error) {
user := about.User
quota := about.StorageQuota

if args.JsonOutput > 0 {
data := map[string]interface{}{
"username": user.DisplayName,
"email": user.EmailAddress,
"used": quota.Usage,
"free": quota.Limit - quota.Usage,
"total": quota.Limit,
"maxuploadsize": about.MaxUploadSize,
}
return jsonOutput(args.Out, args.JsonOutput == 2, data)
}

fmt.Fprintf(args.Out, "User: %s, %s\n", user.DisplayName, user.EmailAddress)
fmt.Fprintf(args.Out, "Used: %s\n", formatSize(quota.Usage, args.SizeInBytes))
fmt.Fprintf(args.Out, "Free: %s\n", formatSize(quota.Limit-quota.Usage, args.SizeInBytes))
Expand Down
4 changes: 2 additions & 2 deletions drive/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (self *Drive) ListChanges(args ListChangesArgs) error {
return nil
}

changeList, err := self.service.Changes.List(args.PageToken).PageSize(args.MaxChanges).RestrictToMyDrive(true).Fields("newStartPageToken", "nextPageToken", "changes(fileId,removed,time,file(id,name,md5Checksum,mimeType,createdTime,modifiedTime))").Do()
changeList, err := self.service.Changes.List(args.PageToken).SupportsAllDrives(true).PageSize(args.MaxChanges).RestrictToMyDrive(true).Fields("newStartPageToken", "nextPageToken", "changes(fileId,removed,time,file(id,name,md5Checksum,mimeType,createdTime,modifiedTime))").Do()
if err != nil {
return fmt.Errorf("Failed listing changes: %s", err)
}
Expand All @@ -43,7 +43,7 @@ func (self *Drive) ListChanges(args ListChangesArgs) error {
}

func (self *Drive) GetChangesStartPageToken() (string, error) {
res, err := self.service.Changes.GetStartPageToken().Do()
res, err := self.service.Changes.GetStartPageToken().SupportsAllDrives(true).Do()
if err != nil {
return "", fmt.Errorf("Failed getting start page token: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions drive/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type DeleteArgs struct {
}

func (self *Drive) Delete(args DeleteArgs) error {
f, err := self.service.Files.Get(args.Id).Fields("name", "mimeType").Do()
f, err := self.service.Files.Get(args.Id).SupportsAllDrives(true).Fields("name", "mimeType").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}
Expand All @@ -21,7 +21,7 @@ func (self *Drive) Delete(args DeleteArgs) error {
return fmt.Errorf("'%s' is a directory, use the 'recursive' flag to delete directories", f.Name)
}

err = self.service.Files.Delete(args.Id).Do()
err = self.service.Files.Delete(args.Id).SupportsAllDrives(true).Do()
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
Expand All @@ -31,7 +31,7 @@ func (self *Drive) Delete(args DeleteArgs) error {
}

func (self *Drive) deleteFile(fileId string) error {
err := self.service.Files.Delete(fileId).Do()
err := self.service.Files.Delete(fileId).SupportsAllDrives(true).Do()
if err != nil {
return fmt.Errorf("Failed to delete file: %s", err)
}
Expand Down
34 changes: 17 additions & 17 deletions drive/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (self *Drive) Download(args DownloadArgs) error {
return self.downloadRecursive(args)
}

f, err := self.service.Files.Get(args.Id).Fields("id", "name", "size", "mimeType", "md5Checksum").Do()
f, err := self.service.Files.Get(args.Id).SupportsAllDrives(true).Fields("id", "name", "size", "mimeType", "md5Checksum").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (self *Drive) DownloadQuery(args DownloadQueryArgs) error {
}

func (self *Drive) downloadRecursive(args DownloadArgs) error {
f, err := self.service.Files.Get(args.Id).Fields("id", "name", "size", "mimeType", "md5Checksum").Do()
f, err := self.service.Files.Get(args.Id).SupportsAllDrives(true).Fields("id", "name", "size", "mimeType", "md5Checksum").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}
Expand All @@ -124,10 +124,24 @@ func (self *Drive) downloadRecursive(args DownloadArgs) error {
}

func (self *Drive) downloadBinary(f *drive.File, args DownloadArgs) (int64, int64, error) {
// Path to file
fpath := filepath.Join(args.Path, f.Name)

// Check if file exists to force
if !args.Skip && !args.Force && fileExists(fpath) {
return 0, 0, fmt.Errorf("File '%s' already exists, use --force to overwrite or --skip to skip", fpath)
}

//Check if file exists to skip
if args.Skip && fileExists(fpath) {
fmt.Printf("File '%s' already exists, skipping\n", fpath)
return 0, 0, nil
}

// Get timeout reader wrapper and context
timeoutReaderWrapper, ctx := getTimeoutReaderWrapperContext(args.Timeout)

res, err := self.service.Files.Get(f.Id).Context(ctx).Download()
res, err := self.service.Files.Get(f.Id).SupportsAllDrives(true).Context(ctx).Download()
if err != nil {
if isTimeoutError(err) {
return 0, 0, fmt.Errorf("Failed to download file: timeout, no data was transferred for %v", args.Timeout)
Expand All @@ -138,9 +152,6 @@ func (self *Drive) downloadBinary(f *drive.File, args DownloadArgs) (int64, int6
// Close body on function exit
defer res.Body.Close()

// Path to file
fpath := filepath.Join(args.Path, f.Name)

if !args.Stdout {
fmt.Fprintf(args.Out, "Downloading %s -> %s\n", f.Name, fpath)
}
Expand Down Expand Up @@ -178,17 +189,6 @@ func (self *Drive) saveFile(args saveFileArgs) (int64, int64, error) {
return 0, 0, err
}

// Check if file exists to force
if !args.skip && !args.force && fileExists(args.fpath) {
return 0, 0, fmt.Errorf("File '%s' already exists, use --force to overwrite or --skip to skip", args.fpath)
}

//Check if file exists to skip
if args.skip && fileExists(args.fpath) {
fmt.Printf("File '%s' already exists, skipping\n", args.fpath)
return 0, 0, nil
}

// Ensure any parent directories exists
if err := mkdir(args.fpath); err != nil {
return 0, 0, err
Expand Down
2 changes: 1 addition & 1 deletion drive/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type ExportArgs struct {
}

func (self *Drive) Export(args ExportArgs) error {
f, err := self.service.Files.Get(args.Id).Fields("name", "mimeType").Do()
f, err := self.service.Files.Get(args.Id).SupportsAllDrives(true).Fields("name", "mimeType").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}
Expand Down
22 changes: 21 additions & 1 deletion drive/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ type FileInfoArgs struct {
Out io.Writer
Id string
SizeInBytes bool
JsonOutput int64
}

func (self *Drive) Info(args FileInfoArgs) error {
f, err := self.service.Files.Get(args.Id).Fields("id", "name", "size", "createdTime", "modifiedTime", "md5Checksum", "mimeType", "parents", "shared", "description", "webContentLink", "webViewLink").Do()
f, err := self.service.Files.Get(args.Id).SupportsAllDrives(true).Fields("id", "name", "size", "createdTime", "modifiedTime", "md5Checksum", "mimeType", "parents", "shared", "description", "webContentLink", "webViewLink").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}
Expand All @@ -24,6 +25,25 @@ func (self *Drive) Info(args FileInfoArgs) error {
return err
}

if args.JsonOutput > 0 {
data := map[string]interface{}{
"Id": f.Id,
"Name": f.Name,
"Path": absPath,
"Description": f.Description,
"Mime": f.MimeType,
"Size": f.Size,
"Created": formatDatetime(f.CreatedTime),
"Modified": formatDatetime(f.ModifiedTime),
"Md5sum": f.Md5Checksum,
"Shared": formatBool(f.Shared),
"Parents": f.Parents,
"ViewUrl": f.WebViewLink,
"DownloadUrl": f.WebContentLink,
}
return jsonOutput(args.Out, args.JsonOutput == 2, data)
}

PrintFileInfo(PrintFileInfoArgs{
Out: args.Out,
File: f,
Expand Down
32 changes: 31 additions & 1 deletion drive/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ListFilesArgs struct {
SkipHeader bool
SizeInBytes bool
AbsPath bool
JsonOutput int64
}

func (self *Drive) List(args ListFilesArgs) (err error) {
Expand All @@ -44,6 +45,13 @@ func (self *Drive) List(args ListFilesArgs) (err error) {
}
}

if args.JsonOutput > 0 {
return OutputFileList(OutputFileListArgs{
Out: args.Out,
Files: files,
JsonOutput: args.JsonOutput,
})
}
PrintFileList(PrintFileListArgs{
Out: args.Out,
Files: files,
Expand Down Expand Up @@ -74,7 +82,7 @@ func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) {

controlledStop := fmt.Errorf("Controlled stop")

err := self.service.Files.List().Q(args.query).Fields(args.fields...).OrderBy(args.sortOrder).PageSize(pageSize).Pages(context.TODO(), func(fl *drive.FileList) error {
err := self.service.Files.List().SupportsAllDrives(true).Corpora("allDrives").IncludeItemsFromAllDrives(true).Q(args.query).Fields(args.fields...).OrderBy(args.sortOrder).PageSize(pageSize).Pages(context.TODO(), func(fl *drive.FileList) error {
files = append(files, fl.Files...)

// Stop when we have all the files we need
Expand All @@ -97,6 +105,28 @@ func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) {
return files, nil
}

type OutputFileListArgs struct {
Out io.Writer
Files []*drive.File
JsonOutput int64
}

func OutputFileList(args OutputFileListArgs) error {
var data []map[string]interface{}

for _, f := range args.Files {
data = append(data, map[string]interface{}{
"id": f.Id,
"name": f.Name,
"type": filetype(f),
"size": f.Size,
"created": formatDatetime(f.CreatedTime),
})
}

return jsonOutput(args.Out, args.JsonOutput == 2, data)
}

type PrintFileListArgs struct {
Out io.Writer
Files []*drive.File
Expand Down
2 changes: 1 addition & 1 deletion drive/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (self *Drive) mkdir(args MkdirArgs) (*drive.File, error) {
dstFile.Parents = args.Parents

// Create directory
f, err := self.service.Files.Create(dstFile).Do()
f, err := self.service.Files.Create(dstFile).SupportsAllDrives(true).Do()
if err != nil {
return nil, fmt.Errorf("Failed to create directory: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion drive/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (self *remotePathfinder) getParent(id string) (*drive.File, error) {
}

// Fetch file from drive
f, err := self.service.Get(id).Fields("id", "name", "parents").Do()
f, err := self.service.Get(id).SupportsAllDrives(true).Fields("id", "name", "parents").Do()
if err != nil {
return nil, fmt.Errorf("Failed to get file: %s", err)
}
Expand Down
28 changes: 28 additions & 0 deletions drive/rename.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package drive

import (
"fmt"
"io"
"google.golang.org/api/drive/v3"
)

type RenameArgs struct {
Out io.Writer
Id string
NewName string
}

func (self *Drive) Rename(args RenameArgs) error {
f, err := self.service.Files.Get(args.Id).Fields("name", "mimeType").Do()
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}

f2, err := self.service.Files.Update(args.Id,&drive.File{Name:args.NewName}).Do()
if err != nil {
return fmt.Errorf("Failed to rename file: %s", err)
}

fmt.Fprintf(args.Out, "Renamed '%s' -> '%s'\n", f.Name, f2.Name)
return nil
}
Loading