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

Commit f727ae8

Browse files
committed
Json output
1 parent 29ca5a9 commit f727ae8

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

drive/about.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package drive
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"text/tabwriter"
@@ -9,6 +10,7 @@ import (
910
type AboutArgs struct {
1011
Out io.Writer
1112
SizeInBytes bool
13+
JsonOutput int64
1214
}
1315

1416
func (self *Drive) About(args AboutArgs) (err error) {
@@ -20,6 +22,22 @@ func (self *Drive) About(args AboutArgs) (err error) {
2022
user := about.User
2123
quota := about.StorageQuota
2224

25+
if args.JsonOutput > 0 {
26+
data := map[string]interface{}{
27+
"username": user.DisplayName,
28+
"email": user.EmailAddress,
29+
"used": quota.Usage,
30+
"free": quota.Limit - quota.Usage,
31+
"total": quota.Limit,
32+
"maxuploadsize": about.MaxUploadSize,
33+
}
34+
enc := json.NewEncoder(args.Out)
35+
if args.JsonOutput == 2 {
36+
enc.SetIndent("", " ")
37+
}
38+
return enc.Encode(&data);
39+
}
40+
2341
fmt.Fprintf(args.Out, "User: %s, %s\n", user.DisplayName, user.EmailAddress)
2442
fmt.Fprintf(args.Out, "Used: %s\n", formatSize(quota.Usage, args.SizeInBytes))
2543
fmt.Fprintf(args.Out, "Free: %s\n", formatSize(quota.Limit-quota.Usage, args.SizeInBytes))

drive/list.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package drive
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"golang.org/x/net/context"
67
"google.golang.org/api/drive/v3"
@@ -18,6 +19,7 @@ type ListFilesArgs struct {
1819
SkipHeader bool
1920
SizeInBytes bool
2021
AbsPath bool
22+
JsonOutput int64
2123
}
2224

2325
func (self *Drive) List(args ListFilesArgs) (err error) {
@@ -44,6 +46,13 @@ func (self *Drive) List(args ListFilesArgs) (err error) {
4446
}
4547
}
4648

49+
if args.JsonOutput > 0 {
50+
return OutputFileList(OutputFileListArgs{
51+
Out: args.Out,
52+
Files: files,
53+
JsonOutput: args.JsonOutput,
54+
})
55+
}
4756
PrintFileList(PrintFileListArgs{
4857
Out: args.Out,
4958
Files: files,
@@ -97,6 +106,31 @@ func (self *Drive) listAllFiles(args listAllFilesArgs) ([]*drive.File, error) {
97106
return files, nil
98107
}
99108

109+
type OutputFileListArgs struct {
110+
Out io.Writer
111+
Files []*drive.File
112+
JsonOutput int64
113+
}
114+
115+
func OutputFileList(args OutputFileListArgs) error {
116+
var data []map[string]interface{}
117+
118+
for _, f := range args.Files {
119+
data = append(data, map[string]interface{}{
120+
"id": f.Id,
121+
"name": f.Name,
122+
"type": filetype(f),
123+
"size": f.Size,
124+
"created": formatDatetime(f.CreatedTime),
125+
})
126+
}
127+
enc := json.NewEncoder(args.Out)
128+
if args.JsonOutput == 2 {
129+
enc.SetIndent("", " ")
130+
}
131+
return enc.Encode(&data);
132+
}
133+
100134
type PrintFileListArgs struct {
101135
Out io.Writer
102136
Files []*drive.File

gdrive.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const DefaultTimeout = 5 * 60
1919
const DefaultQuery = "trashed = false and 'me' in owners"
2020
const DefaultShareRole = "reader"
2121
const DefaultShareType = "anyone"
22+
const DefaultJsonOutput = 0
2223

2324
var DefaultConfigDir = GetDefaultConfigDir()
2425

@@ -96,6 +97,12 @@ func main() {
9697
Description: "Size in bytes",
9798
OmitValue: true,
9899
},
100+
cli.IntFlag{
101+
Name: "jsonOutput",
102+
Patterns: []string{"--jsonOutput"},
103+
Description: "Print json output (1: normal, 2: pretty)",
104+
DefaultValue: DefaultJsonOutput,
105+
},
99106
),
100107
},
101108
},
@@ -810,6 +817,12 @@ func main() {
810817
Description: "Show size in bytes",
811818
OmitValue: true,
812819
},
820+
cli.IntFlag{
821+
Name: "jsonOutput",
822+
Patterns: []string{"--jsonOutput"},
823+
Description: "Print json output (1: normal, 2: pretty)",
824+
DefaultValue: DefaultJsonOutput,
825+
},
813826
),
814827
},
815828
},

handlers_drive.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func listHandler(ctx cli.Context) {
3030
SkipHeader: args.Bool("skipHeader"),
3131
SizeInBytes: args.Bool("sizeInBytes"),
3232
AbsPath: args.Bool("absPath"),
33+
JsonOutput: args.Int64("jsonOutput"),
3334
})
3435
checkErr(err)
3536
}
@@ -320,6 +321,7 @@ func aboutHandler(ctx cli.Context) {
320321
err := newDrive(args).About(drive.AboutArgs{
321322
Out: os.Stdout,
322323
SizeInBytes: args.Bool("sizeInBytes"),
324+
JsonOutput: args.Int64("jsonOutput"),
323325
})
324326
checkErr(err)
325327
}

0 commit comments

Comments
 (0)