Skip to content

Commit

Permalink
Return error
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jan 17, 2016
1 parent c88aba0 commit d4d1b00
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 60 deletions.
7 changes: 5 additions & 2 deletions drive/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ type AboutArgs struct {
ExportFormats bool
}

func (self *Drive) About(args AboutArgs) {
func (self *Drive) About(args AboutArgs) (err error) {
about, err := self.service.About.Get().Fields("exportFormats", "importFormats", "maxImportSizes", "maxUploadSize", "storageQuota", "user").Do()
errorF(err, "Failed to get about %s", err)
if err != nil {
return fmt.Errorf("Failed to get about: %s", err)
}

if args.ExportFormats {
printSupportedFormats(about.ExportFormats)
Expand All @@ -34,6 +36,7 @@ func (self *Drive) About(args AboutArgs) {
fmt.Printf("Free: %s\n", formatSize(quota.Limit - quota.UsageInDrive, args.SizeInBytes))
fmt.Printf("Total: %s\n", formatSize(quota.Limit, args.SizeInBytes))
fmt.Printf("Max upload size: %s\n", formatSize(about.MaxUploadSize, args.SizeInBytes))
return
}

func printSupportedFormats(formats map[string][]string) {
Expand Down
12 changes: 9 additions & 3 deletions drive/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ type DeleteArgs struct {
Id string
}

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

err = self.service.Files.Delete(args.Id).Do()
errorF(err, "Failed to delete file")
if err != nil {
return fmt.Errorf("Failed to delete file", err)
}

fmt.Printf("Removed file '%s'\n", f.Name)
return
}
25 changes: 17 additions & 8 deletions drive/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,52 @@ type DownloadFileArgs struct {
Stdout bool
}

func (self *Drive) Download(args DownloadFileArgs) {
func (self *Drive) Download(args DownloadFileArgs) (err error) {
getFile := self.service.Files.Get(args.Id)

f, err := getFile.Do()
errorF(err, "Failed to get file: %s", err)
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}

res, err := getFile.Download()
errorF(err, "Failed to download file: %s", err)
if err != nil {
return fmt.Errorf("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
_, err := io.Copy(os.Stdout, res.Body)
return err
}

// Check if file exists
if !args.Force && fileExists(f.Name) {
exitF("File '%s' already exists, use --force to overwrite", f.Name)
return fmt.Errorf("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)
if err != nil {
return fmt.Errorf("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)
if err != nil {
return fmt.Errorf("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)
//}
return
}
8 changes: 6 additions & 2 deletions drive/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ type FileInfoArgs struct {
SizeInBytes bool
}

func (self *Drive) Info(args FileInfoArgs) {
func (self *Drive) Info(args FileInfoArgs) (err error) {
f, err := self.service.Files.Get(args.Id).Fields("id", "name", "size", "createdTime", "modifiedTime", "md5Checksum", "mimeType", "parents", "shared", "description").Do()
errorF(err, "Failed to get file: %s", err)
if err != nil {
return fmt.Errorf("Failed to get file: %s", err)
}

PrintFileInfo(PrintFileInfoArgs{
File: f,
SizeInBytes: args.SizeInBytes,
})

return
}

type PrintFileInfoArgs struct {
Expand Down
8 changes: 6 additions & 2 deletions drive/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ type ListFilesArgs struct {
SizeInBytes bool
}

func (self *Drive) List(args ListFilesArgs) {
func (self *Drive) List(args ListFilesArgs) (err error) {
fileList, err := self.service.Files.List().PageSize(args.MaxFiles).Q(args.Query).Fields("nextPageToken", "files(id,name,size,createdTime)").Do()
errorF(err, "Failed listing files: %s\n", err)
if err != nil {
return fmt.Errorf("Failed listing files: %s", err)
}

PrintFileList(PrintFileListArgs{
Files: fileList.Files,
NameWidth: int(args.NameWidth),
SkipHeader: args.SkipHeader,
SizeInBytes: args.SizeInBytes,
})

return
}

type PrintFileListArgs struct {
Expand Down
8 changes: 6 additions & 2 deletions drive/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package drive

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

const DirectoryMimeType = "application/vnd.google-apps.folder"
Expand All @@ -12,7 +13,7 @@ type MkdirArgs struct {
Share bool
}

func (self *Drive) Mkdir(args MkdirArgs) {
func (self *Drive) Mkdir(args MkdirArgs) (err error) {
dstFile := &drive.File{Name: args.Name, MimeType: DirectoryMimeType}

// Set parent folder if provided
Expand All @@ -22,11 +23,14 @@ func (self *Drive) Mkdir(args MkdirArgs) {

// Create folder
f, err := self.service.Files.Create(dstFile).Do()
errorF(err, "Failed to create folder: %s", err)
if err != nil {
return fmt.Errorf("Failed to create folder: %s", err)
}

PrintFileInfo(PrintFileInfoArgs{File: f})

//if args.Share {
// self.Share(TODO)
//}
return
}
13 changes: 9 additions & 4 deletions drive/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ type ShareArgs struct {
Revoke bool
}

func (self *Drive) Share(args ShareArgs) {
func (self *Drive) Share(args ShareArgs) (err error) {
if args.Revoke {
err := self.deletePermissions(args)
errorF(err, "Failed delete permissions: %s", err)
err = self.deletePermissions(args)
if err != nil {
return fmt.Errorf("Failed delete permissions: %s", err)
}
}

permission := &drive.Permission{
Expand All @@ -28,9 +30,12 @@ func (self *Drive) Share(args ShareArgs) {
}

p, err := self.service.Permissions.Create(args.FileId, permission).Do()
errorF(err, "Failed share file: %s", err)
if err != nil {
return fmt.Errorf("Failed share file: %s", err)
}

fmt.Println(p)
return
}

func (self *Drive) deletePermissions(args ShareArgs) error {
Expand Down
15 changes: 11 additions & 4 deletions drive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ type UploadFileArgs struct {
Share bool
}

func (self *Drive) Upload(args UploadFileArgs) {
func (self *Drive) Upload(args UploadFileArgs) (err error) {
//if args.Stdin {
// self.uploadStdin()
//}

srcFile, err := os.Open(args.Path)
errorF(err, "Failed to open file: %s", err)
if err != nil {
return fmt.Errorf("Failed to open file: %s", err)
}

srcFileInfo, err := srcFile.Stat()
errorF(err, "Failed to read file metadata: %s", err)
if err != nil {
return fmt.Errorf("Failed to read file metadata: %s", err)
}

// Instantiate empty drive file
dstFile := &drive.File{}
Expand All @@ -53,10 +57,13 @@ func (self *Drive) Upload(args UploadFileArgs) {
}

f, err := self.service.Files.Create(dstFile).ResumableMedia(context.Background(), srcFile, srcFileInfo.Size(), dstFile.MimeType).Do()
errorF(err, "Failed to upload file: %s", err)
if err != nil {
return fmt.Errorf("Failed to upload file: %s", err)
}

fmt.Printf("Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size)
//if args.Share {
// self.Share(TODO)
//}
return
}
16 changes: 0 additions & 16 deletions drive/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,6 @@ func truncateString(str string, maxRunes int) string {
return truncated
}

func errorF(err error, format string, a ...interface{}) {
if err == nil {
return
}

fmt.Fprintf(os.Stderr, format, a...)
fmt.Println("")
os.Exit(1)
}

func exitF(format string, a ...interface{}) {
fmt.Fprintf(os.Stderr, format, a...)
fmt.Println("")
os.Exit(1)
}

func fileExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
Expand Down
33 changes: 16 additions & 17 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,30 @@ const TokenFilename = "token_v2.json"

func listHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).List(drive.ListFilesArgs{
err := newDrive(args).List(drive.ListFilesArgs{
MaxFiles: args.Int64("maxFiles"),
NameWidth: args.Int64("nameWidth"),
Query: args.String("query"),
SkipHeader: args.Bool("skipHeader"),
SizeInBytes: args.Bool("sizeInBytes"),
})
checkErr(err)
}

func downloadHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Download(drive.DownloadFileArgs{
err := newDrive(args).Download(drive.DownloadFileArgs{
Id: args.String("id"),
Force: args.Bool("force"),
Stdout: args.Bool("stdout"),
NoProgress: args.Bool("noprogress"),
})
checkErr(err)
}

func uploadHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Upload(drive.UploadFileArgs{
err := newDrive(args).Upload(drive.UploadFileArgs{
Path: args.String("path"),
Name: args.String("name"),
Parent: args.String("parent"),
Expand All @@ -48,43 +47,43 @@ func uploadHandler(ctx cli.Context) {
Stdin: args.Bool("stdin"),
Share: args.Bool("share"),
})
checkErr(err)
}

func infoHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Info(drive.FileInfoArgs{
err := newDrive(args).Info(drive.FileInfoArgs{
Id: args.String("id"),
SizeInBytes: args.Bool("sizeInBytes"),
})
checkErr(err)
}

func mkdirHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Mkdir(drive.MkdirArgs{
err := newDrive(args).Mkdir(drive.MkdirArgs{
Name: args.String("name"),
Parent: args.String("parent"),
Share: args.Bool("share"),
})
checkErr(err)
}

func shareHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Share(drive.ShareArgs{
err := newDrive(args).Share(drive.ShareArgs{
FileId: args.String("id"),
Role: args.String("role"),
Type: args.String("type"),
Email: args.String("email"),
Discoverable: args.Bool("discoverable"),
Revoke: args.Bool("revoke"),
})
checkErr(err)
}

func urlHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Url(drive.UrlArgs{
FileId: args.String("id"),
DownloadUrl: args.Bool("download"),
Expand All @@ -93,20 +92,20 @@ func urlHandler(ctx cli.Context) {

func deleteHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).Delete(drive.DeleteArgs{
err := newDrive(args).Delete(drive.DeleteArgs{
Id: args.String("id"),
})
checkErr(err)
}

func aboutHandler(ctx cli.Context) {
args := ctx.Args()

newDrive(args).About(drive.AboutArgs{
err := newDrive(args).About(drive.AboutArgs{
SizeInBytes: args.Bool("sizeInBytes"),
ImportFormats: args.Bool("importFormats"),
ExportFormats: args.Bool("exportFormats"),
})
checkErr(err)
}

func printVersion(ctx cli.Context) {
Expand Down
7 changes: 7 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ func ExitF(format string, a ...interface{}) {
fmt.Println("")
os.Exit(1)
}

func checkErr(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

0 comments on commit d4d1b00

Please sign in to comment.