aboutsummaryrefslogtreecommitdiffstats
path: root/drive/download.go
diff options
context:
space:
mode:
authorPetter Rasmussen2016-01-17 21:13:46 +0100
committerPetter Rasmussen2016-01-17 21:13:46 +0100
commitf35fd0892688ff638b30dcd48fdd56b9e2627cf1 (patch)
tree18439804fdb5c87e4ba0f4cea4561db35bdd7868 /drive/download.go
parentf16b89b6f6bee6023c51b4f8120a3e4776128384 (diff)
downloadgdrive-f35fd0892688ff638b30dcd48fdd56b9e2627cf1.tar.bz2
One file per command
Diffstat (limited to 'drive/download.go')
-rw-r--r--drive/download.go55
1 files changed, 55 insertions, 0 deletions
diff --git a/drive/download.go b/drive/download.go
new file mode 100644
index 0000000..9a35912
--- /dev/null
+++ b/drive/download.go
@@ -0,0 +1,55 @@
+package drive
+
+import (
+ "fmt"
+ "io"
+ "os"
+)
+
+type DownloadFileArgs struct {
+ Id string
+ Force bool
+ NoProgress bool
+ Stdout bool
+}
+
+func (self *Drive) Download(args DownloadFileArgs) {
+ getFile := self.service.Files.Get(args.Id)
+
+ f, err := getFile.Do()
+ errorF(err, "Failed to get file: %s", err)
+
+ res, err := getFile.Download()
+ errorF(err, "Failed to download file: %s", err)
+
+ // Close body on function exit
+ defer res.Body.Close()
+
+ if args.Stdout {
+ // Write file content to stdout
+ io.Copy(os.Stdout, res.Body)
+ return
+ }
+
+ // Check if file exists
+ if !args.Force && fileExists(f.Name) {
+ exitF("File '%s' already exists, use --force to overwrite", f.Name)
+ }
+
+ // Create new file
+ outFile, err := os.Create(f.Name)
+ errorF(err, "Unable to create new file: %s", err)
+
+ // Close file on function exit
+ defer outFile.Close()
+
+ // Save file to disk
+ bytes, err := io.Copy(outFile, res.Body)
+ errorF(err, "Failed saving file: %s", err)
+
+ fmt.Printf("Downloaded '%s' at %s, total %d\n", f.Name, "x/s", bytes)
+
+ //if deleteSourceFile {
+ // self.Delete(args.Id)
+ //}
+}