-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdf.go
112 lines (99 loc) · 3.09 KB
/
gdf.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"github.com/dustin/go-humanize"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/drive/v3"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
)
// Quick & dirty CLI tool to see if a file exists in Google Drive
//
// Usage:
// $ gdf <string or strings>
//
// Example Usage:
// $ gdf Queen Elizabeth II
// | 3.9 MB| Queen Elizabeth II (popart).png
// $ gdf foobarbaz.png
// No files found.
//
// Notes:
// 1) The service account will need to be granted 'drivescope' in the Admin Console. To grant this, go to
// 'Admin Console --> Security --> Advanced Settings --> Manage API Client Access'
// 2) Drive API will need to be enabled in the service account's GCP project
// 3) Service account will need to be granted Domain-wide Delegation authority.
// See https://developers.google.com/admin-sdk/directory/v1/guides/delegation for more info.
const (
credsfile string = "credentials.json"
drivescope string = "https://www.googleapis.com/auth/drive.readonly"
pagesize int64 = 1000
who string = "you@you.com"
)
// Get an authenticated http client
func httpclient(creds []byte) (*http.Client, error) {
conf, err := google.JWTConfigFromJSON(creds, drivescope)
if err != nil {
return nil, err
}
conf.Subject = who
return conf.Client(oauth2.NoContext), nil
}
func main() {
// Were any filename search criteria specified as command line argument(s)?
if len(os.Args) < 2 {
fmt.Printf("error: no search criteria specified.\nUsage:\n\t$ gdf <string or strings>\n\n")
return
}
// Load the service account JSON credentials file
creds, err := ioutil.ReadFile(credsfile)
if err != nil {
log.Fatalf("error reading credentials file: %s", err)
}
// Get an authenticated http client
client, err := httpclient(creds)
if err != nil {
log.Fatalf("error creating authenticated http client: %s", err)
}
// Get a Drive client
dc, err := drive.New(client)
if err != nil {
log.Fatalf("error creating Google Drive client: %v", err)
}
// Search Drive for files with filenames containing argv[1:]
files, err := dc.Files.List().
Fields("nextPageToken, files(id, mimeType, modifiedTime, name, size)").
PageSize(pagesize).
Q("name contains '" + strings.Join(os.Args[1:], " ") + "' and trashed = false").
SupportsAllDrives(true).
Do()
if err != nil {
log.Fatalf("error reading files list from Google Drive: %v", err)
}
// Did any filenames match the specified search criteria?
if len(files.Files) == 0 {
fmt.Println("0 files found.")
return
}
// Range through the list of files
for _, i := range files.Files {
// Get the last modified time
lmt, err := time.Parse(time.RFC3339, i.ModifiedTime)
if err != nil {
log.Fatalf("error converting last modified time: %s", err)
}
if i.MimeType == "application/vnd.google-apps.folder" {
fmt.Printf("|%8s |%14s | %s\n", "folder", humanize.Time(lmt), i.Name)
} else {
fmt.Printf("|%8s |%14s | %s\n", humanize.Bytes(uint64(i.Size)), humanize.Time(lmt), i.Name)
}
}
fmt.Printf("%d files found.\n", len(files.Files))
return
}
// EOF