diff options
| author | Petter Rasmussen | 2016-01-18 21:35:08 +0100 |
|---|---|---|
| committer | Petter Rasmussen | 2016-01-18 21:35:08 +0100 |
| commit | 33716c2a43f92466401e08533308219fb520af9e (patch) | |
| tree | 60b64640ed9f42698c9791d756dfcc13b160b1d2 | |
| parent | d4d1b00c9609a4d493f79bdd74bae5dc60d37ed7 (diff) | |
| download | gdrive-33716c2a43f92466401e08533308219fb520af9e.tar.bz2 | |
Take output writer as argument
| -rw-r--r-- | drive/about.go | 21 | ||||
| -rw-r--r-- | drive/delete.go | 4 | ||||
| -rw-r--r-- | drive/download.go | 3 | ||||
| -rw-r--r-- | drive/info.go | 6 | ||||
| -rw-r--r-- | drive/list.go | 7 | ||||
| -rw-r--r-- | drive/mkdir.go | 4 | ||||
| -rw-r--r-- | drive/share.go | 4 | ||||
| -rw-r--r-- | drive/upload.go | 4 | ||||
| -rw-r--r-- | drive/url.go | 6 | ||||
| -rw-r--r-- | handlers.go | 10 |
10 files changed, 49 insertions, 20 deletions
diff --git a/drive/about.go b/drive/about.go index bea9fda..dea927c 100644 --- a/drive/about.go +++ b/drive/about.go @@ -1,12 +1,13 @@ package drive import ( + "io" "fmt" - "os" "text/tabwriter" ) type AboutArgs struct { + Out io.Writer SizeInBytes bool ImportFormats bool ExportFormats bool @@ -19,29 +20,29 @@ func (self *Drive) About(args AboutArgs) (err error) { } if args.ExportFormats { - printSupportedFormats(about.ExportFormats) + printSupportedFormats(args.Out, about.ExportFormats) return } if args.ImportFormats { - printSupportedFormats(about.ImportFormats) + printSupportedFormats(args.Out, about.ImportFormats) return } user := about.User quota := about.StorageQuota - fmt.Printf("User: %s, %s\n", user.DisplayName, user.EmailAddress) - fmt.Printf("Used: %s\n", formatSize(quota.UsageInDrive, args.SizeInBytes)) - 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)) + fmt.Fprintf(args.Out, "User: %s, %s\n", user.DisplayName, user.EmailAddress) + fmt.Fprintf(args.Out, "Used: %s\n", formatSize(quota.UsageInDrive, args.SizeInBytes)) + fmt.Fprintf(args.Out, "Free: %s\n", formatSize(quota.Limit - quota.UsageInDrive, args.SizeInBytes)) + fmt.Fprintf(args.Out, "Total: %s\n", formatSize(quota.Limit, args.SizeInBytes)) + fmt.Fprintf(args.Out, "Max upload size: %s\n", formatSize(about.MaxUploadSize, args.SizeInBytes)) return } -func printSupportedFormats(formats map[string][]string) { +func printSupportedFormats(out io.Writer, formats map[string][]string) { w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 0, 3, ' ', 0) + w.Init(out, 0, 0, 3, ' ', 0) fmt.Fprintln(w, "From\tTo") diff --git a/drive/delete.go b/drive/delete.go index 6e868eb..cc0aeb3 100644 --- a/drive/delete.go +++ b/drive/delete.go @@ -1,10 +1,12 @@ package drive import ( + "io" "fmt" ) type DeleteArgs struct { + Out io.Writer Id string } @@ -19,6 +21,6 @@ func (self *Drive) Delete(args DeleteArgs) (err error) { return fmt.Errorf("Failed to delete file", err) } - fmt.Printf("Removed file '%s'\n", f.Name) + fmt.Fprintf(args.Out, "Removed file '%s'\n", f.Name) return } diff --git a/drive/download.go b/drive/download.go index 57e0160..31bcc58 100644 --- a/drive/download.go +++ b/drive/download.go @@ -7,6 +7,7 @@ import ( ) type DownloadFileArgs struct { + Out io.Writer Id string Force bool NoProgress bool @@ -55,7 +56,7 @@ func (self *Drive) Download(args DownloadFileArgs) (err error) { return fmt.Errorf("Failed saving file: %s", err) } - fmt.Printf("Downloaded '%s' at %s, total %d\n", f.Name, "x/s", bytes) + fmt.Fprintf(args.Out, "Downloaded '%s' at %s, total %d\n", f.Name, "x/s", bytes) //if deleteSourceFile { // self.Delete(args.Id) diff --git a/drive/info.go b/drive/info.go index ca09d0a..080c23c 100644 --- a/drive/info.go +++ b/drive/info.go @@ -1,11 +1,13 @@ package drive import ( + "io" "fmt" "google.golang.org/api/drive/v3" ) type FileInfoArgs struct { + Out io.Writer Id string SizeInBytes bool } @@ -17,6 +19,7 @@ func (self *Drive) Info(args FileInfoArgs) (err error) { } PrintFileInfo(PrintFileInfoArgs{ + Out: args.Out, File: f, SizeInBytes: args.SizeInBytes, }) @@ -25,6 +28,7 @@ func (self *Drive) Info(args FileInfoArgs) (err error) { } type PrintFileInfoArgs struct { + Out io.Writer File *drive.File SizeInBytes bool } @@ -47,7 +51,7 @@ func PrintFileInfo(args PrintFileInfoArgs) { for _, item := range items { if item.value() != "" { - fmt.Printf("%s: %s\n", item.key(), item.value()) + fmt.Fprintf(args.Out, "%s: %s\n", item.key(), item.value()) } } } diff --git a/drive/list.go b/drive/list.go index b2e3662..5649a12 100644 --- a/drive/list.go +++ b/drive/list.go @@ -2,12 +2,13 @@ package drive import ( "fmt" - "os" + "io" "text/tabwriter" "google.golang.org/api/drive/v3" ) type ListFilesArgs struct { + Out io.Writer MaxFiles int64 NameWidth int64 Query string @@ -22,6 +23,7 @@ func (self *Drive) List(args ListFilesArgs) (err error) { } PrintFileList(PrintFileListArgs{ + Out: args.Out, Files: fileList.Files, NameWidth: int(args.NameWidth), SkipHeader: args.SkipHeader, @@ -32,6 +34,7 @@ func (self *Drive) List(args ListFilesArgs) (err error) { } type PrintFileListArgs struct { + Out io.Writer Files []*drive.File NameWidth int SkipHeader bool @@ -40,7 +43,7 @@ type PrintFileListArgs struct { func PrintFileList(args PrintFileListArgs) { w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 0, 3, ' ', 0) + w.Init(args.Out, 0, 0, 3, ' ', 0) if !args.SkipHeader { fmt.Fprintln(w, "Id\tName\tSize\tCreated") diff --git a/drive/mkdir.go b/drive/mkdir.go index 4080474..99047ee 100644 --- a/drive/mkdir.go +++ b/drive/mkdir.go @@ -2,12 +2,14 @@ package drive import ( "google.golang.org/api/drive/v3" + "io" "fmt" ) const DirectoryMimeType = "application/vnd.google-apps.folder" type MkdirArgs struct { + Out io.Writer Name string Parent string Share bool @@ -27,7 +29,7 @@ func (self *Drive) Mkdir(args MkdirArgs) (err error) { return fmt.Errorf("Failed to create folder: %s", err) } - PrintFileInfo(PrintFileInfoArgs{File: f}) + PrintFileInfo(PrintFileInfoArgs{Out: args.Out, File: f}) //if args.Share { // self.Share(TODO) diff --git a/drive/share.go b/drive/share.go index 37c1bf8..43655df 100644 --- a/drive/share.go +++ b/drive/share.go @@ -1,11 +1,13 @@ package drive import ( + "io" "fmt" "google.golang.org/api/drive/v3" ) type ShareArgs struct { + Out io.Writer FileId string Role string Type string @@ -34,7 +36,7 @@ func (self *Drive) Share(args ShareArgs) (err error) { return fmt.Errorf("Failed share file: %s", err) } - fmt.Println(p) + fmt.Fprintln(args.Out, p) return } diff --git a/drive/upload.go b/drive/upload.go index 16ae940..3a65d0f 100644 --- a/drive/upload.go +++ b/drive/upload.go @@ -4,12 +4,14 @@ import ( "fmt" "mime" "os" + "io" "path/filepath" "google.golang.org/api/drive/v3" "golang.org/x/net/context" ) type UploadFileArgs struct { + Out io.Writer Path string Name string Parent string @@ -61,7 +63,7 @@ func (self *Drive) Upload(args UploadFileArgs) (err error) { return fmt.Errorf("Failed to upload file: %s", err) } - fmt.Printf("Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size) + fmt.Fprintf(args.Out, "Uploaded '%s' at %s, total %d\n", f.Name, "x/s", f.Size) //if args.Share { // self.Share(TODO) //} diff --git a/drive/url.go b/drive/url.go index 2dc429d..9dee4ad 100644 --- a/drive/url.go +++ b/drive/url.go @@ -1,20 +1,22 @@ package drive import ( + "io" "fmt" ) type UrlArgs struct { + Out io.Writer FileId string DownloadUrl bool } func (self *Drive) Url(args UrlArgs) { if args.DownloadUrl { - fmt.Println(downloadUrl(args.FileId)) + fmt.Fprintln(args.Out, downloadUrl(args.FileId)) return } - fmt.Println(previewUrl(args.FileId)) + fmt.Fprintln(args.Out, previewUrl(args.FileId)) } func previewUrl(id string) string { diff --git a/handlers.go b/handlers.go index 5ff5cdb..a36444c 100644 --- a/handlers.go +++ b/handlers.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "os" "strings" "./cli" "./client" @@ -16,6 +17,7 @@ const TokenFilename = "token_v2.json" func listHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).List(drive.ListFilesArgs{ + Out: os.Stdout, MaxFiles: args.Int64("maxFiles"), NameWidth: args.Int64("nameWidth"), Query: args.String("query"), @@ -28,6 +30,7 @@ func listHandler(ctx cli.Context) { func downloadHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Download(drive.DownloadFileArgs{ + Out: os.Stdout, Id: args.String("id"), Force: args.Bool("force"), Stdout: args.Bool("stdout"), @@ -39,6 +42,7 @@ func downloadHandler(ctx cli.Context) { func uploadHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Upload(drive.UploadFileArgs{ + Out: os.Stdout, Path: args.String("path"), Name: args.String("name"), Parent: args.String("parent"), @@ -53,6 +57,7 @@ func uploadHandler(ctx cli.Context) { func infoHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Info(drive.FileInfoArgs{ + Out: os.Stdout, Id: args.String("id"), SizeInBytes: args.Bool("sizeInBytes"), }) @@ -62,6 +67,7 @@ func infoHandler(ctx cli.Context) { func mkdirHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Mkdir(drive.MkdirArgs{ + Out: os.Stdout, Name: args.String("name"), Parent: args.String("parent"), Share: args.Bool("share"), @@ -72,6 +78,7 @@ func mkdirHandler(ctx cli.Context) { func shareHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Share(drive.ShareArgs{ + Out: os.Stdout, FileId: args.String("id"), Role: args.String("role"), Type: args.String("type"), @@ -85,6 +92,7 @@ func shareHandler(ctx cli.Context) { func urlHandler(ctx cli.Context) { args := ctx.Args() newDrive(args).Url(drive.UrlArgs{ + Out: os.Stdout, FileId: args.String("id"), DownloadUrl: args.Bool("download"), }) @@ -93,6 +101,7 @@ func urlHandler(ctx cli.Context) { func deleteHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).Delete(drive.DeleteArgs{ + Out: os.Stdout, Id: args.String("id"), }) checkErr(err) @@ -101,6 +110,7 @@ func deleteHandler(ctx cli.Context) { func aboutHandler(ctx cli.Context) { args := ctx.Args() err := newDrive(args).About(drive.AboutArgs{ + Out: os.Stdout, SizeInBytes: args.Bool("sizeInBytes"), ImportFormats: args.Bool("importFormats"), ExportFormats: args.Bool("exportFormats"), |
