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

Commit

Permalink
Json output
Browse files Browse the repository at this point in the history
  • Loading branch information
Elbandi committed Jul 25, 2019
1 parent 29ca5a9 commit f727ae8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
18 changes: 18 additions & 0 deletions drive/about.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package drive

import (
"encoding/json"
"fmt"
"io"
"text/tabwriter"
Expand All @@ -9,6 +10,7 @@ import (
type AboutArgs struct {
Out io.Writer
SizeInBytes bool
JsonOutput int64
}

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

import (
"encoding/json"
"fmt"
"golang.org/x/net/context"
"google.golang.org/api/drive/v3"
Expand All @@ -18,6 +19,7 @@ type ListFilesArgs struct {
SkipHeader bool
SizeInBytes bool
AbsPath bool
JsonOutput int64
}

func (self *Drive) List(args ListFilesArgs) (err error) {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 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 @@ -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,
},
),
},
},
Expand Down
2 changes: 2 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 @@ -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)
}
Expand Down

0 comments on commit f727ae8

Please sign in to comment.