forked from prasmussen/gdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
share.go
111 lines (92 loc) · 2.37 KB
/
share.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
package drive
import (
"fmt"
"google.golang.org/api/drive/v3"
"io"
"text/tabwriter"
)
type ShareArgs struct {
Out io.Writer
FileId string
Role string
Type string
Email string
Domain string
Discoverable bool
}
func (self *Drive) Share(args ShareArgs) error {
permission := &drive.Permission{
AllowFileDiscovery: args.Discoverable,
Role: args.Role,
Type: args.Type,
EmailAddress: args.Email,
Domain: args.Domain,
}
_, err := self.service.Permissions.Create(args.FileId, permission).Do()
if err != nil {
return fmt.Errorf("Failed to share file: %s", err)
}
fmt.Fprintf(args.Out, "Granted %s permission to %s\n", args.Role, args.Type)
return nil
}
type RevokePermissionArgs struct {
Out io.Writer
FileId string
PermissionId string
}
func (self *Drive) RevokePermission(args RevokePermissionArgs) error {
err := self.service.Permissions.Delete(args.FileId, args.PermissionId).Do()
if err != nil {
fmt.Errorf("Failed to revoke permission: %s", err)
return err
}
fmt.Fprintf(args.Out, "Permission revoked\n")
return nil
}
type ListPermissionsArgs struct {
Out io.Writer
FileId string
}
func (self *Drive) ListPermissions(args ListPermissionsArgs) error {
permList, err := self.service.Permissions.List(args.FileId).Fields("permissions(id,role,type,domain,emailAddress,allowFileDiscovery)").Do()
if err != nil {
fmt.Errorf("Failed to list permissions: %s", err)
return err
}
printPermissions(printPermissionsArgs{
out: args.Out,
permissions: permList.Permissions,
})
return nil
}
func (self *Drive) shareAnyoneReader(fileId string) error {
permission := &drive.Permission{
Role: "reader",
Type: "anyone",
}
_, err := self.service.Permissions.Create(fileId, permission).Do()
if err != nil {
return fmt.Errorf("Failed to share file: %s", err)
}
return nil
}
type printPermissionsArgs struct {
out io.Writer
permissions []*drive.Permission
}
func printPermissions(args printPermissionsArgs) {
w := new(tabwriter.Writer)
w.Init(args.out, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "Id\tType\tRole\tEmail\tDomain\tDiscoverable")
for _, p := range args.permissions {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\n",
p.Id,
p.Type,
p.Role,
p.EmailAddress,
p.Domain,
formatBool(p.AllowFileDiscovery),
)
}
w.Flush()
}