-
Notifications
You must be signed in to change notification settings - Fork 0
/
recursivels.go
67 lines (55 loc) · 1.34 KB
/
recursivels.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"fmt"
"log"
"./auth"
"google.golang.org/api/drive/v2"
)
// https://developers.google.com/drive/v2/reference/files
var (
queryFlag = flag.String("query", "", "Query for the root of the tree")
idFlag = flag.String("id", "", "File Id for the root of the tree")
)
// Lists files on google drive.
func main() {
flag.Parse()
if *queryFlag == "" && *idFlag == "" {
log.Fatal("Either --id or --query is required.")
}
client, err := auth.DoAuth()
if err != nil {
log.Fatal(err)
}
query := *queryFlag
if *idFlag != "" {
query = fmt.Sprintf(`'%s' in parents`, *idFlag)
}
svc, err := drive.New(client)
if err != nil {
log.Fatalf("An error occurred opening driveservice: %v\n", err)
}
processQuery(svc, query)
}
func processQuery(svc *drive.Service, query string) {
list, err := svc.Files.List().Q(query).Do()
if err != nil {
log.Fatalf("An error occurred listing files: %v\n", err)
}
var folders []string
for _, item := range list.Items {
if item.MimeType == "application/vnd.google-apps.folder" {
folders = append(folders, item.Id)
continue
}
printFile(item)
}
for _, item := range folders {
query := fmt.Sprintf(`'%s' in parents`, item)
processQuery(svc, query)
}
}
func printFile(file *drive.File) {
// TODO: Print more useful things
fmt.Printf("%s %s\n", file.Id, file.Title)
}