forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.go
170 lines (138 loc) · 6.09 KB
/
drive.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"fmt"
"github.com/prasmussen/gdrive/cli"
"github.com/prasmussen/gdrive/gdrive"
"github.com/prasmussen/gdrive/util"
"github.com/prasmussen/google-api-go-client/googleapi"
"github.com/voxelbrain/goptions"
"os"
)
const (
VersionNumber = "1.9.0"
)
type Options struct {
Advanced bool `goptions:"-a, --advanced, description='Advanced Mode -- lets you specify your own oauth client id and secret on setup'"`
AppPath string `goptions:"-c, --config, description='Set application path where config and token is stored. Defaults to ~/.gdrive'"`
Version bool `goptions:"-v, --version, description='Print version'"`
goptions.Help `goptions:"-h, --help, description='Show this help'"`
goptions.Verbs
List struct {
MaxResults int `goptions:"-m, --max, description='Max results'"`
IncludeDocs bool `goptions:"--include-docs, description='Include google docs in listing'"`
TitleFilter string `goptions:"-t, --title, mutexgroup='query', description='Title filter'"`
Query string `goptions:"-q, --query, mutexgroup='query', description='Query (see https://developers.google.com/drive/search-parameters)'"`
SharedStatus bool `goptions:"-s, --shared, description='Show shared status (Note: this will generate 1 http req per file)'"`
NoHeader bool `goptions:"-n, --noheader, description='Do not show the header'"`
SizeInBytes bool `goptions:"--bytes, description='Show size in bytes'"`
} `goptions:"list"`
Info struct {
FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
SizeInBytes bool `goptions:"--bytes, description='Show size in bytes'"`
} `goptions:"info"`
Folder struct {
Title string `goptions:"-t, --title, obligatory, description='Folder to create'"`
ParentId string `goptions:"-p, --parent, description='Parent Id of the folder'"`
Share bool `goptions:"--share, description='Share created folder'"`
} `goptions:"folder"`
Upload struct {
File *os.File `goptions:"-f, --file, mutexgroup='input', obligatory, rdonly, description='File or directory to upload'"`
Stdin bool `goptions:"-s, --stdin, mutexgroup='input', obligatory, description='Use stdin as file content'"`
Title string `goptions:"-t, --title, description='Title to give uploaded file. Defaults to filename'"`
ParentId string `goptions:"-p, --parent, description='Parent Id of the file'"`
Share bool `goptions:"--share, description='Share uploaded file'"`
MimeType string `goptions:"--mimetype, description='The MIME type (default will try to figure it out)'"`
Convert bool `goptions:"--convert, description='File will be converted to Google Docs format'"`
ChunkSize int64 `goptions:"-C, --chunksize, description='Set chunk size in bytes. Minimum is 262144, default is 4194304. Recommended to be a power of two.'"`
} `goptions:"upload"`
Download struct {
FileId string `goptions:"-i, --id, mutexgroup='download', obligatory, description='File Id'"`
Format string `goptions:"--format, description='Download file in a specified format (needed for google docs)'"`
Stdout bool `goptions:"-s, --stdout, description='Write file content to stdout'"`
Force bool `goptions:"--force, description='Overwrite existing file'"`
Pop bool `goptions:"--pop, mutexgroup='download', description='Download latest file, and remove it from google drive'"`
} `goptions:"download"`
Delete struct {
FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
} `goptions:"delete"`
Share struct {
FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
} `goptions:"share"`
Unshare struct {
FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
} `goptions:"unshare"`
Url struct {
FileId string `goptions:"-i, --id, obligatory, description='File Id'"`
Preview bool `goptions:"-p, --preview, mutexgroup='urltype', description='Generate preview url (default)'"`
Download bool `goptions:"-d, --download, mutexgroup='urltype', description='Generate download url'"`
} `goptions:"url"`
Quota struct {
SizeInBytes bool `goptions:"--bytes, description='Show size in bytes'"`
} `goptions:"quota"`
}
func main() {
opts := &Options{}
goptions.ParseAndFail(opts)
// Print version number and exit if the version flag is set
if opts.Version {
fmt.Printf("gdrive v%s\n", VersionNumber)
return
}
// Get authorized drive client
drive, err := gdrive.New(opts.AppPath, opts.Advanced, true)
if err != nil {
writeError("An error occurred creating Drive client: %v\n", err)
}
switch opts.Verbs {
case "list":
args := opts.List
err = cli.List(drive, args.Query, args.TitleFilter, args.MaxResults, args.SharedStatus, args.NoHeader, args.IncludeDocs, args.SizeInBytes)
case "info":
err = cli.Info(drive, opts.Info.FileId, opts.Info.SizeInBytes)
case "folder":
args := opts.Folder
err = cli.Folder(drive, args.Title, args.ParentId, args.Share)
case "upload":
args := opts.Upload
// Set custom chunksize if given
if args.ChunkSize >= (1 << 18) {
googleapi.SetChunkSize(args.ChunkSize)
}
if args.Stdin {
err = cli.UploadStdin(drive, os.Stdin, args.Title, args.ParentId, args.Share, args.MimeType, args.Convert)
} else {
err = cli.Upload(drive, args.File, args.Title, args.ParentId, args.Share, args.MimeType, args.Convert)
}
case "download":
args := opts.Download
if args.Pop {
err = cli.DownloadLatest(drive, args.Stdout, args.Format, args.Force)
} else {
err = cli.Download(drive, args.FileId, args.Stdout, false, args.Format, args.Force)
}
case "delete":
err = cli.Delete(drive, opts.Delete.FileId)
case "share":
err = cli.Share(drive, opts.Share.FileId)
case "unshare":
err = cli.Unshare(drive, opts.Unshare.FileId)
case "url":
if opts.Url.Download {
fmt.Println(util.DownloadUrl(opts.Url.FileId))
} else {
fmt.Println(util.PreviewUrl(opts.Url.FileId))
}
case "quota":
err = cli.Quota(drive, opts.Quota.SizeInBytes)
default:
goptions.PrintHelp()
}
if err != nil {
writeError("%s", err)
}
}
func writeError(format string, err error) {
fmt.Fprintf(os.Stderr, format, err)
fmt.Print("\n")
os.Exit(1)
}