From f727ae87d78bbf5a34c22ded638157acbe9928c5 Mon Sep 17 00:00:00 2001 From: Elbandi Date: Thu, 25 Jul 2019 16:40:49 +0200 Subject: [PATCH] Json output --- drive/about.go | 18 ++++++++++++++++++ drive/list.go | 34 ++++++++++++++++++++++++++++++++++ gdrive.go | 13 +++++++++++++ handlers_drive.go | 2 ++ 4 files changed, 67 insertions(+) diff --git a/drive/about.go b/drive/about.go index c2f1643d..72ad08dc 100644 --- a/drive/about.go +++ b/drive/about.go @@ -1,6 +1,7 @@ package drive import ( + "encoding/json" "fmt" "io" "text/tabwriter" @@ -9,6 +10,7 @@ import ( type AboutArgs struct { Out io.Writer SizeInBytes bool + JsonOutput int64 } func (self *Drive) About(args AboutArgs) (err error) { @@ -20,6 +22,22 @@ func (self *Drive) About(args AboutArgs) (err error) { user := about.User quota := about.StorageQuota + if args.JsonOutput > 0 { + data := map[string]interface{}{ + "username": user.DisplayName, + "email": user.EmailAddress, + "used": quota.Usage, + "free": quota.Limit - quota.Usage, + "total": quota.Limit, + "maxuploadsize": about.MaxUploadSize, + } + enc := json.NewEncoder(args.Out) + if args.JsonOutput == 2 { + enc.SetIndent("", " ") + } + return enc.Encode(&data); + } + fmt.Fprintf(args.Out, "User: %s, %s\n", user.DisplayName, user.EmailAddress) fmt.Fprintf(args.Out, "Used: %s\n", formatSize(quota.Usage, args.SizeInBytes)) fmt.Fprintf(args.Out, "Free: %s\n", formatSize(quota.Limit-quota.Usage, args.SizeInBytes)) diff --git a/drive/list.go b/drive/list.go index ab8aca56..f3d26ceb 100644 --- a/drive/list.go +++ b/drive/list.go @@ -1,6 +1,7 @@ package drive import ( + "encoding/json" "fmt" "golang.org/x/net/context" "google.golang.org/api/drive/v3" @@ -18,6 +19,7 @@ type ListFilesArgs struct { SkipHeader bool SizeInBytes bool AbsPath bool + JsonOutput int64 } func (self *Drive) List(args ListFilesArgs) (err error) { @@ -44,6 +46,13 @@ func (self *Drive) List(args ListFilesArgs) (err error) { } } + if args.JsonOutput > 0 { + return OutputFileList(OutputFileListArgs{ + Out: args.Out, + Files: files, + JsonOutput: args.JsonOutput, + }) + } PrintFileList(PrintFileListArgs{ Out: args.Out, Files: files, @@ -97,6 +106,31 @@ func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) { return files, nil } +type OutputFileListArgs struct { + Out io.Writer + Files []*drive.File + JsonOutput int64 +} + +func OutputFileList(args OutputFileListArgs) error { + var data []map[string]interface{} + + for _, f := range args.Files { + data = append(data, map[string]interface{}{ + "id": f.Id, + "name": f.Name, + "type": filetype(f), + "size": f.Size, + "created": formatDatetime(f.CreatedTime), + }) + } + enc := json.NewEncoder(args.Out) + if args.JsonOutput == 2 { + enc.SetIndent("", " ") + } + return enc.Encode(&data); +} + type PrintFileListArgs struct { Out io.Writer Files []*drive.File diff --git a/gdrive.go b/gdrive.go index c1a817e7..ffca2345 100644 --- a/gdrive.go +++ b/gdrive.go @@ -19,6 +19,7 @@ const DefaultTimeout = 5 * 60 const DefaultQuery = "trashed = false and 'me' in owners" const DefaultShareRole = "reader" const DefaultShareType = "anyone" +const DefaultJsonOutput = 0 var DefaultConfigDir = GetDefaultConfigDir() @@ -96,6 +97,12 @@ func main() { Description: "Size in bytes", OmitValue: true, }, + cli.IntFlag{ + Name: "jsonOutput", + Patterns: []string{"--jsonOutput"}, + Description: "Print json output (1: normal, 2: pretty)", + DefaultValue: DefaultJsonOutput, + }, ), }, }, @@ -810,6 +817,12 @@ func main() { Description: "Show size in bytes", OmitValue: true, }, + cli.IntFlag{ + Name: "jsonOutput", + Patterns: []string{"--jsonOutput"}, + Description: "Print json output (1: normal, 2: pretty)", + DefaultValue: DefaultJsonOutput, + }, ), }, }, diff --git a/handlers_drive.go b/handlers_drive.go index 7bda872f..8b12b536 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -30,6 +30,7 @@ func listHandler(ctx cli.Context) { SkipHeader: args.Bool("skipHeader"), SizeInBytes: args.Bool("sizeInBytes"), AbsPath: args.Bool("absPath"), + JsonOutput: args.Int64("jsonOutput"), }) checkErr(err) } @@ -320,6 +321,7 @@ func aboutHandler(ctx cli.Context) { err := newDrive(args).About(drive.AboutArgs{ Out: os.Stdout, SizeInBytes: args.Bool("sizeInBytes"), + JsonOutput: args.Int64("jsonOutput"), }) checkErr(err) }