From f727ae87d78bbf5a34c22ded638157acbe9928c5 Mon Sep 17 00:00:00 2001 From: Elbandi Date: Thu, 25 Jul 2019 16:40:49 +0200 Subject: [PATCH 1/4] 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) } From ab7bfca58cfe5cceb6c0c1237fa23f9eda828ea5 Mon Sep 17 00:00:00 2001 From: Elbandi Date: Thu, 25 Jul 2019 16:59:03 +0200 Subject: [PATCH 2/4] Use a json helper function --- drive/about.go | 7 +------ drive/list.go | 8 ++------ drive/util.go | 10 ++++++++++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/drive/about.go b/drive/about.go index 72ad08dc..738f78a9 100644 --- a/drive/about.go +++ b/drive/about.go @@ -1,7 +1,6 @@ package drive import ( - "encoding/json" "fmt" "io" "text/tabwriter" @@ -31,11 +30,7 @@ func (self *Drive) About(args AboutArgs) (err error) { "total": quota.Limit, "maxuploadsize": about.MaxUploadSize, } - enc := json.NewEncoder(args.Out) - if args.JsonOutput == 2 { - enc.SetIndent("", " ") - } - return enc.Encode(&data); + return jsonOutput(args.Out, args.JsonOutput == 2, data) } fmt.Fprintf(args.Out, "User: %s, %s\n", user.DisplayName, user.EmailAddress) diff --git a/drive/list.go b/drive/list.go index f3d26ceb..99c37d0e 100644 --- a/drive/list.go +++ b/drive/list.go @@ -1,7 +1,6 @@ package drive import ( - "encoding/json" "fmt" "golang.org/x/net/context" "google.golang.org/api/drive/v3" @@ -124,11 +123,8 @@ func OutputFileList(args OutputFileListArgs) error { "created": formatDatetime(f.CreatedTime), }) } - enc := json.NewEncoder(args.Out) - if args.JsonOutput == 2 { - enc.SetIndent("", " ") - } - return enc.Encode(&data); + + return jsonOutput(args.Out, args.JsonOutput == 2, data) } type PrintFileListArgs struct { diff --git a/drive/util.go b/drive/util.go index 181b9b90..1d4d356e 100644 --- a/drive/util.go +++ b/drive/util.go @@ -1,7 +1,9 @@ package drive import ( + "encoding/json" "fmt" + "io" "math" "os" "path/filepath" @@ -56,6 +58,14 @@ func round(n float64) int64 { return int64(math.Floor(n + 0.5)) } +func jsonOutput(out io.Writer, indent bool, data interface{}) error { + enc := json.NewEncoder(out) + if indent { + enc.SetIndent("", " ") + } + return enc.Encode(&data); +} + func formatBool(b bool) string { return strings.Title(strconv.FormatBool(b)) } From 30c0cec477470cd8ddf72eb9801d91f38cc2797a Mon Sep 17 00:00:00 2001 From: Elbandi Date: Thu, 25 Jul 2019 17:13:25 +0200 Subject: [PATCH 3/4] Add info command json output --- drive/info.go | 20 ++++++++++++++++++++ gdrive.go | 6 ++++++ handlers_drive.go | 1 + 3 files changed, 27 insertions(+) diff --git a/drive/info.go b/drive/info.go index c6f44715..b58dc9c6 100644 --- a/drive/info.go +++ b/drive/info.go @@ -10,6 +10,7 @@ type FileInfoArgs struct { Out io.Writer Id string SizeInBytes bool + JsonOutput int64 } func (self *Drive) Info(args FileInfoArgs) error { @@ -24,6 +25,25 @@ func (self *Drive) Info(args FileInfoArgs) error { return err } + if args.JsonOutput > 0 { + data := map[string]interface{}{ + "Id": f.Id, + "Name": f.Name, + "Path": absPath, + "Description": f.Description, + "Mime": f.MimeType, + "Size": f.Size, + "Created": formatDatetime(f.CreatedTime), + "Modified": formatDatetime(f.ModifiedTime), + "Md5sum": f.Md5Checksum, + "Shared": formatBool(f.Shared), + "Parents": f.Parents, + "ViewUrl": f.WebViewLink, + "DownloadUrl": f.WebContentLink, + } + return jsonOutput(args.Out, args.JsonOutput == 2, data) + } + PrintFileInfo(PrintFileInfoArgs{ Out: args.Out, File: f, diff --git a/gdrive.go b/gdrive.go index ffca2345..8de09d05 100644 --- a/gdrive.go +++ b/gdrive.go @@ -378,6 +378,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 8b12b536..e3c4c3ea 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -190,6 +190,7 @@ func infoHandler(ctx cli.Context) { Out: os.Stdout, Id: args.String("fileId"), SizeInBytes: args.Bool("sizeInBytes"), + JsonOutput: args.Int64("jsonOutput"), }) checkErr(err) } From 56b2a165cee7e70c0c0969ac291d3551625987d5 Mon Sep 17 00:00:00 2001 From: Elbandi Date: Thu, 25 Jul 2019 17:19:15 +0200 Subject: [PATCH 4/4] Add revision list command json output --- drive/revision_list.go | 30 ++++++++++++++++++++++++++++++ gdrive.go | 6 ++++++ handlers_drive.go | 1 + 3 files changed, 37 insertions(+) diff --git a/drive/revision_list.go b/drive/revision_list.go index eec4dab7..d4a4e42d 100644 --- a/drive/revision_list.go +++ b/drive/revision_list.go @@ -13,6 +13,7 @@ type ListRevisionsArgs struct { NameWidth int64 SkipHeader bool SizeInBytes bool + JsonOutput int64 } func (self *Drive) ListRevisions(args ListRevisionsArgs) (err error) { @@ -21,6 +22,13 @@ func (self *Drive) ListRevisions(args ListRevisionsArgs) (err error) { return fmt.Errorf("Failed listing revisions: %s", err) } + if args.JsonOutput > 0 { + return OutputRevisionList(OutputRevisionListArgs{ + Out: args.Out, + Revisions: revList.Revisions, + JsonOutput: args.JsonOutput, + }) + } PrintRevisionList(PrintRevisionListArgs{ Out: args.Out, Revisions: revList.Revisions, @@ -32,6 +40,28 @@ func (self *Drive) ListRevisions(args ListRevisionsArgs) (err error) { return } +type OutputRevisionListArgs struct { + Out io.Writer + Revisions []*drive.Revision + JsonOutput int64 +} + +func OutputRevisionList(args OutputRevisionListArgs) error { + var data []map[string]interface{} + + for _, rev := range args.Revisions { + data = append(data, map[string]interface{}{ + "id": rev.Id, + "name": rev.OriginalFilename, + "size": rev.Size, + "modified": formatDatetime(rev.ModifiedTime), + "forever": rev.KeepForever, + }) + } + + return jsonOutput(args.Out, args.JsonOutput == 2, data) +} + type PrintRevisionListArgs struct { Out io.Writer Revisions []*drive.Revision diff --git a/gdrive.go b/gdrive.go index 8de09d05..5fbc8289 100644 --- a/gdrive.go +++ b/gdrive.go @@ -707,6 +707,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, + }, ), }, }, diff --git a/handlers_drive.go b/handlers_drive.go index e3c4c3ea..731801f9 100644 --- a/handlers_drive.go +++ b/handlers_drive.go @@ -227,6 +227,7 @@ func listRevisionsHandler(ctx cli.Context) { NameWidth: args.Int64("nameWidth"), SizeInBytes: args.Bool("sizeInBytes"), SkipHeader: args.Bool("skipHeader"), + JsonOutput: args.Int64("jsonOutput"), }) checkErr(err) }