Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

[WIP] Support JSON output #481

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions drive/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
type AboutArgs struct {
Out io.Writer
SizeInBytes bool
JsonOutput int64
}

func (self *Drive) About(args AboutArgs) (err error) {
Expand All @@ -20,6 +21,18 @@ 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,
}
return jsonOutput(args.Out, args.JsonOutput == 2, 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))
Expand Down
20 changes: 20 additions & 0 deletions drive/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type FileInfoArgs struct {
Out io.Writer
Id string
SizeInBytes bool
JsonOutput int64
}

func (self *Drive) Info(args FileInfoArgs) error {
Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions drive/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ListFilesArgs struct {
SkipHeader bool
SizeInBytes bool
AbsPath bool
JsonOutput int64
}

func (self *Drive) List(args ListFilesArgs) (err error) {
Expand All @@ -44,6 +45,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,
Expand Down Expand Up @@ -97,6 +105,28 @@ 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),
})
}

return jsonOutput(args.Out, args.JsonOutput == 2, data)
}

type PrintFileListArgs struct {
Out io.Writer
Files []*drive.File
Expand Down
30 changes: 30 additions & 0 deletions drive/revision_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ListRevisionsArgs struct {
NameWidth int64
SkipHeader bool
SizeInBytes bool
JsonOutput int64
}

func (self *Drive) ListRevisions(args ListRevisionsArgs) (err error) {
Expand All @@ -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,
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions drive/util.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package drive

import (
"encoding/json"
"fmt"
"io"
"math"
"os"
"path/filepath"
Expand Down Expand Up @@ -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))
}
Expand Down
25 changes: 25 additions & 0 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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,
},
),
},
},
Expand Down Expand Up @@ -371,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,
},
),
},
},
Expand Down Expand Up @@ -694,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,
},
),
},
},
Expand Down Expand Up @@ -810,6 +829,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,
},
),
},
},
Expand Down
4 changes: 4 additions & 0 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -189,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)
}
Expand Down Expand Up @@ -225,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)
}
Expand Down Expand Up @@ -320,6 +323,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)
}
Expand Down