diff options
| author | Bart van der Schans | 2015-05-19 17:41:16 +0200 |
|---|---|---|
| committer | Bart van der Schans | 2015-05-19 17:41:16 +0200 |
| commit | 126355ea2c8914f9c5138aa78c3392d0effe35aa (patch) | |
| tree | 1d0a7ae981a393554e89edd97cb8b38c3f452df6 /cli | |
| parent | 24950968ecea619378a36edff78e46fee0eb3a43 (diff) | |
| download | gdrive-126355ea2c8914f9c5138aa78c3392d0effe35aa.tar.bz2 | |
Add option for exporting google docs and overwriting existing files
Diffstat (limited to 'cli')
| -rw-r--r-- | cli/cli.go | 37 |
1 files changed, 25 insertions, 12 deletions
@@ -118,6 +118,8 @@ func printInfo(d *gdrive.Drive, f *drive.File) { "Md5sum": f.Md5Checksum, "Shared": util.FormatBool(isShared(d, f.Id)), "Parents": util.ParentList(f.Parents), + "Download": f.DownloadUrl, + "Export": f.ExportLinks["text/csv"], } order := []string{ @@ -131,6 +133,8 @@ func printInfo(d *gdrive.Drive, f *drive.File) { "Md5sum", "Shared", "Parents", + "Download", + "Export", } util.Print(fields, order) } @@ -309,7 +313,7 @@ func uploadFile(d *gdrive.Drive, input *os.File, inputInfo os.FileInfo, title st return err } -func DownloadLatest(d *gdrive.Drive, stdout bool) error { +func DownloadLatest(d *gdrive.Drive, stdout bool, format string, force bool) error { list, err := d.Files.List().Do() if err != nil { return err @@ -320,29 +324,36 @@ func DownloadLatest(d *gdrive.Drive, stdout bool) error { } latestId := list.Items[0].Id - return Download(d, latestId, stdout, true) + return Download(d, latestId, stdout, true, format, force) } // Download file from drive -func Download(d *gdrive.Drive, fileId string, stdout, deleteAfterDownload bool) error { +func Download(d *gdrive.Drive, fileId string, stdout, deleteAfterDownload bool, format string, force bool) error { // Get file info info, err := d.Files.Get(fileId).Do() if err != nil { return fmt.Errorf("An error occurred: %v\n", err) } - if info.DownloadUrl == "" { - // If there is no DownloadUrl, there is no body - return fmt.Errorf("An error occurred: File is not downloadable") + downloadUrl := info.DownloadUrl + extension := "" + + if downloadUrl == "" && format == "" { + return fmt.Errorf("Document has no download url. Try to specify an export format.") + } + + downloadUrl, extension, err = util.ExportFormat(info, format) + if err != nil { + return err } // Measure transfer rate getRate := util.MeasureTransferRate() // GET the download url - res, err := d.Client().Get(info.DownloadUrl) + res, err := d.Client().Get(downloadUrl) if err != nil { - return fmt.Errorf("An error occurred: %v\n", err) + return fmt.Errorf("An error occurred: %v", err) } // Close body on function exit @@ -354,13 +365,15 @@ func Download(d *gdrive.Drive, fileId string, stdout, deleteAfterDownload bool) return nil } + fileName := fmt.Sprintf("%s%s", info.Title, extension) + // Check if file exists - if util.FileExists(info.Title) { - return fmt.Errorf("An error occurred: '%s' already exists\n", info.Title) + if !force && util.FileExists(fileName) { + return fmt.Errorf("An error occurred: '%s' already exists", fileName) } // Create a new file - outFile, err := os.Create(info.Title) + outFile, err := os.Create(fileName) if err != nil { return fmt.Errorf("An error occurred: %v\n", err) } @@ -374,7 +387,7 @@ func Download(d *gdrive.Drive, fileId string, stdout, deleteAfterDownload bool) return fmt.Errorf("An error occurred: %s", err) } - fmt.Printf("Downloaded '%s' at %s, total %s\n", info.Title, getRate(bytes), util.FileSizeFormat(bytes)) + fmt.Printf("Downloaded '%s' at %s, total %s\n", fileName, getRate(bytes), util.FileSizeFormat(bytes)) if deleteAfterDownload { err = Delete(d, fileId) |
