Skip to content

Commit

Permalink
Take output writer as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Jan 18, 2016
1 parent d4d1b00 commit 33716c2
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 20 deletions.
21 changes: 11 additions & 10 deletions drive/about.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package drive

import (
"io"
"fmt"
"os"
"text/tabwriter"
)

type AboutArgs struct {
Out io.Writer
SizeInBytes bool
ImportFormats bool
ExportFormats bool
Expand All @@ -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")

Expand Down
4 changes: 3 additions & 1 deletion drive/delete.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package drive

import (
"io"
"fmt"
)

type DeleteArgs struct {
Out io.Writer
Id string
}

Expand All @@ -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
}
3 changes: 2 additions & 1 deletion drive/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
)

type DownloadFileArgs struct {
Out io.Writer
Id string
Force bool
NoProgress bool
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion drive/info.go
Original file line number Diff line number Diff line change
@@ -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
}
Expand All @@ -17,6 +19,7 @@ func (self *Drive) Info(args FileInfoArgs) (err error) {
}

PrintFileInfo(PrintFileInfoArgs{
Out: args.Out,
File: f,
SizeInBytes: args.SizeInBytes,
})
Expand All @@ -25,6 +28,7 @@ func (self *Drive) Info(args FileInfoArgs) (err error) {
}

type PrintFileInfoArgs struct {
Out io.Writer
File *drive.File
SizeInBytes bool
}
Expand All @@ -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())
}
}
}
7 changes: 5 additions & 2 deletions drive/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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")
Expand Down
4 changes: 3 additions & 1 deletion drive/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion drive/share.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 3 additions & 1 deletion drive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
//}
Expand Down
6 changes: 4 additions & 2 deletions drive/url.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"os"
"strings"
"./cli"
"./client"
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
})
Expand All @@ -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"),
Expand All @@ -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"),
Expand All @@ -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"),
})
Expand All @@ -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)
Expand All @@ -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"),
Expand Down

0 comments on commit 33716c2

Please sign in to comment.