aboutsummaryrefslogtreecommitdiffstats
path: root/cli
diff options
context:
space:
mode:
authorTravis Reeder2014-09-15 14:42:55 -0700
committerTravis Reeder2014-09-15 14:42:55 -0700
commit18b353d561629acb86eeafa6a0a7773ef0ab7d63 (patch)
tree41e0e5b7d0dde8bc94ca25180ae70022ea95bc5a /cli
parent63169fef51dab3ca4f55775640bfe5b202fcc042 (diff)
parentd415b4dfd5fa64478ed6da21ed2a85f023c9f74c (diff)
downloadgdrive-18b353d561629acb86eeafa6a0a7773ef0ab7d63.tar.bz2
Merge pull request #1 from treeder/upload-folder
Upload folder
Diffstat (limited to 'cli')
-rw-r--r--cli/cli.go69
1 files changed, 62 insertions, 7 deletions
diff --git a/cli/cli.go b/cli/cli.go
index 4e347fb..d67109b 100644
--- a/cli/cli.go
+++ b/cli/cli.go
@@ -136,6 +136,16 @@ func printInfo(d *gdrive.Drive, f *drive.File) {
// Create folder in drive
func Folder(d *gdrive.Drive, title string, parentId string, share bool) error {
+ info, err := makeFolder(d, title, parentId, share)
+ if err != nil {
+ return err
+ }
+ printInfo(d, info)
+ fmt.Printf("Folder '%s' created\n", info.Title)
+ return nil
+}
+
+func makeFolder(d *gdrive.Drive, title string, parentId string, share bool) (*drive.File, error) {
// File instance
f := &drive.File{Title: title, MimeType: "application/vnd.google-apps.folder"}
// Set parent (if provided)
@@ -146,24 +156,69 @@ func Folder(d *gdrive.Drive, title string, parentId string, share bool) error {
// Create folder
info, err := d.Files.Insert(f).Do()
if err != nil {
- return fmt.Errorf("An error occurred creating the folder: %v\n", err)
+ return nil, fmt.Errorf("An error occurred creating the folder: %v\n", err)
}
// Share folder if the share flag was provided
if share {
Share(d, info.Id)
}
- printInfo(d, info)
- fmt.Printf("Folder '%s' created\n", info.Title)
- return nil
+ return info, err
}
// Upload file to drive
func Upload(d *gdrive.Drive, input io.ReadCloser, title string, parentId string, share bool, mimeType string, convert bool) error {
// Use filename or 'untitled' as title if no title is specified
+ f2, ok := input.(*os.File)
if title == "" {
- if f, ok := input.(*os.File); ok && input != os.Stdin {
- title = filepath.Base(f.Name())
+ if ok && input != os.Stdin {
+ // then find title if it's a file or upload directory if it's a directory (directory can't have a title).
+ fi, err := f2.Stat()
+ if err != nil {
+ return err
+ }
+ if fi.Mode().IsDir() {
+ // then upload the entire directory, calling Upload recursively
+ // make dir first
+ folder, err := makeFolder(d, filepath.Base(f2.Name()), parentId, share)
+ if err != nil {
+ return err
+ }
+ currDir, err := os.Getwd()
+ if err != nil {
+ return err
+ }
+
+ files, err := f2.Readdir(0)
+ if err != nil {
+ return err
+ }
+ // need to change dirs to get the files in the dir
+ err = f2.Chdir()
+ if err != nil {
+ return err
+ }
+ for _, el := range files {
+ if el.IsDir() {
+ // todo: recursively do this, would need to keep track of parent ids for new directories
+ } else {
+ f2, err := os.Open(el.Name())
+ if err != nil {
+ return err
+ }
+ Upload(d, f2, filepath.Base(el.Name()), folder.Id, share, mimeType, convert)
+ }
+ }
+ // go back to previous dir
+ err = os.Chdir(currDir)
+ if err != nil {
+ return err
+ }
+ return nil
+ }
+ // normal file, not a directory
+ title = filepath.Base(f2.Name())
+
} else {
title = "untitled"
}
@@ -185,7 +240,7 @@ func Upload(d *gdrive.Drive, input io.ReadCloser, title string, parentId string,
if convert {
fmt.Printf("Converting to Google Docs format enabled\n")
}
-
+
info, err := d.Files.Insert(f).Convert(convert).Media(input).Do()
if err != nil {
return fmt.Errorf("An error occurred uploading the document: %v\n", err)