aboutsummaryrefslogtreecommitdiffstats
path: root/cli
diff options
context:
space:
mode:
authorPetter Rasmussen2015-05-23 13:53:19 +0200
committerPetter Rasmussen2015-05-23 13:53:19 +0200
commited6dc064c3d4f08e6046236e67994c5cb27d426e (patch)
treebca4b0472f169e7d0d817dac3234397dea466110 /cli
parent24950968ecea619378a36edff78e46fee0eb3a43 (diff)
parent6d19099fc57e3b7cced66dd6fb07b894075a9d6c (diff)
downloadgdrive-ed6dc064c3d4f08e6046236e67994c5cb27d426e.tar.bz2
Merge pull request #61 from schans/master
Add option to export google docs
Diffstat (limited to 'cli')
-rw-r--r--cli/cli.go33
1 files changed, 21 insertions, 12 deletions
diff --git a/cli/cli.go b/cli/cli.go
index b931733..d713f41 100644
--- a/cli/cli.go
+++ b/cli/cli.go
@@ -309,7 +309,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 +320,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 +361,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 +383,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)