Skip to content

Commit

Permalink
Add more share commands, upload + share
Browse files Browse the repository at this point in the history
  • Loading branch information
prasmussen committed Feb 15, 2016
1 parent cd62c9d commit 713b062
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 70 deletions.
2 changes: 0 additions & 2 deletions drive/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type ImportArgs struct {
Out io.Writer
Progress io.Writer
Path string
Share bool
Parents []string
}

Expand All @@ -39,7 +38,6 @@ func (self *Drive) Import(args ImportArgs) error {
Path: args.Path,
Parents: args.Parents,
Mime: toMimes[0],
Share: args.Share,
})
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions drive/mkdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type MkdirArgs struct {
Out io.Writer
Name string
Parents []string
Share bool
}

func (self *Drive) Mkdir(args MkdirArgs) error {
Expand All @@ -36,9 +35,5 @@ func (self *Drive) mkdir(args MkdirArgs) (*drive.File, error) {
return nil, fmt.Errorf("Failed to create directory: %s", err)
}

//if args.Share {
// self.share(TODO)
//}

return f, nil
}
95 changes: 71 additions & 24 deletions drive/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package drive
import (
"io"
"fmt"
"text/tabwriter"
"google.golang.org/api/drive/v3"
)

Expand All @@ -13,50 +14,96 @@ type ShareArgs struct {
Type string
Email string
Discoverable bool
Revoke bool
}

func (self *Drive) Share(args ShareArgs) (err error) {
if args.Revoke {
err = self.deletePermissions(args)
if err != nil {
return fmt.Errorf("Failed delete permissions: %s", err)
}
}

func (self *Drive) Share(args ShareArgs) error {
permission := &drive.Permission{
AllowFileDiscovery: args.Discoverable,
Role: args.Role,
Type: args.Type,
EmailAddress: args.Email,
}

p, err := self.service.Permissions.Create(args.FileId, permission).Do()
_, 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 {
return fmt.Errorf("Failed share file: %s", err)
fmt.Errorf("Failed to revoke permission: %s", err)
return err
}

fmt.Fprintln(args.Out, p)
return
fmt.Fprintf(args.Out, "Permission revoked\n")
return nil
}

type ListPermissionsArgs struct {
Out io.Writer
FileId string
}

func (self *Drive) deletePermissions(args ShareArgs) error {
permList, err := self.service.Permissions.List(args.FileId).Do()
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
}

for _, p := range permList.Permissions {
// Skip owner permissions
if p.Role == "owner" {
continue
}
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.Delete(args.FileId, p.Id).Do()
if err != nil {
return err
}
_, 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()
}
1 change: 0 additions & 1 deletion drive/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ type UpdateArgs struct {
Parents []string
Mime string
Recursive bool
Share bool
ChunkSize int64
}

Expand Down
31 changes: 22 additions & 9 deletions drive/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ func (self *Drive) Upload(args UploadArgs) error {

f, rate, err := self.uploadFile(args)
fmt.Fprintf(args.Out, "Uploaded %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(f.Size, false))
return err

if args.Share {
err = self.shareAnyoneReader(f.Id)
if err != nil {
return err
}

fmt.Fprintf(args.Out, "File is readable by anyone at %s\n", f.WebContentLink)
}
return nil
}

func (self *Drive) uploadRecursive(args UploadArgs) error {
Expand Down Expand Up @@ -76,7 +85,6 @@ func (self *Drive) uploadDirectory(args UploadArgs) error {
Out: args.Out,
Name: srcFileInfo.Name(),
Parents: args.Parents,
Share: args.Share,
})
if err != nil {
return err
Expand Down Expand Up @@ -142,7 +150,7 @@ func (self *Drive) uploadFile(args UploadArgs) (*drive.File, int64, error) {
fmt.Fprintf(args.Out, "Uploading %s\n", args.Path)
started := time.Now()

f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "md5Checksum").Media(srcReader, chunkSize).Do()
f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "md5Checksum", "webContentLink").Media(srcReader, chunkSize).Do()
if err != nil {
return nil, 0, fmt.Errorf("Failed to upload file: %s", err)
}
Expand All @@ -164,7 +172,7 @@ type UploadStreamArgs struct {
Progress io.Writer
}

func (self *Drive) UploadStream(args UploadStreamArgs) (err error) {
func (self *Drive) UploadStream(args UploadStreamArgs) error {
if args.ChunkSize > intMax() - 1 {
return fmt.Errorf("Chunk size is to big, max chunk size for this computer is %d", intMax() - 1)
}
Expand All @@ -189,7 +197,7 @@ func (self *Drive) UploadStream(args UploadStreamArgs) (err error) {
fmt.Fprintf(args.Out, "Uploading %s\n", dstFile.Name)
started := time.Now()

f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size").Media(srcReader, chunkSize).Do()
f, err := self.service.Files.Create(dstFile).Fields("id", "name", "size", "webContentLink").Media(srcReader, chunkSize).Do()
if err != nil {
return fmt.Errorf("Failed to upload file: %s", err)
}
Expand All @@ -198,8 +206,13 @@ func (self *Drive) UploadStream(args UploadStreamArgs) (err error) {
rate := calcRate(f.Size, started, time.Now())

fmt.Fprintf(args.Out, "Uploaded %s at %s/s, total %s\n", f.Id, formatSize(rate, false), formatSize(f.Size, false))
//if args.Share {
// self.Share(TODO)
//}
return
if args.Share {
err = self.shareAnyoneReader(f.Id)
if err != nil {
return err
}

fmt.Fprintf(args.Out, "File is readable by anyone at %s\n", f.WebContentLink)
}
return nil
}
48 changes: 23 additions & 25 deletions gdrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,6 @@ func main() {
Patterns: []string{"--mime"},
Description: "Force mime type",
},
cli.BoolFlag{
Name: "share",
Patterns: []string{"--share"},
Description: "Share file",
OmitValue: true,
},
cli.IntFlag{
Name: "chunksize",
Patterns: []string{"--chunksize"},
Expand Down Expand Up @@ -273,12 +267,6 @@ func main() {
Patterns: []string{"-p", "--parent"},
Description: "Parent id of created directory, can be specified multiple times to give many parents",
},
cli.BoolFlag{
Name: "share",
Patterns: []string{"--share"},
Description: "Share created directory",
OmitValue: true,
},
),
},
},
Expand All @@ -289,12 +277,6 @@ func main() {
FlagGroups: cli.FlagGroups{
cli.NewFlagGroup("global", globalFlags...),
cli.NewFlagGroup("options",
cli.BoolFlag{
Name: "discoverable",
Patterns: []string{"--discoverable"},
Description: "Make file discoverable by search engines",
OmitValue: true,
},
cli.StringFlag{
Name: "role",
Patterns: []string{"--role"},
Expand All @@ -312,15 +294,37 @@ func main() {
Patterns: []string{"--email"},
Description: "The email address of the user or group to share the file with. Requires 'user' or 'group' as type",
},
cli.BoolFlag{
Name: "discoverable",
Patterns: []string{"--discoverable"},
Description: "Make file discoverable by search engines",
OmitValue: true,
},
cli.BoolFlag{
Name: "revoke",
Patterns: []string{"--revoke"},
Description: "Delete all sharing permissions",
Description: "Delete all sharing permissions (owner roles will be skipped)",
OmitValue: true,
},
),
},
},
&cli.Handler{
Pattern: "[global] share list <fileId>",
Description: "List files permissions",
Callback: shareListHandler,
FlagGroups: cli.FlagGroups{
cli.NewFlagGroup("global", globalFlags...),
},
},
&cli.Handler{
Pattern: "[global] share revoke <fileId> <permissionId>",
Description: "Revoke permission",
Callback: shareRevokeHandler,
FlagGroups: cli.FlagGroups{
cli.NewFlagGroup("global", globalFlags...),
},
},
&cli.Handler{
Pattern: "[global] delete [options] <id>",
Description: "Delete file or directory",
Expand Down Expand Up @@ -611,12 +615,6 @@ func main() {
Description: "Hide progress",
OmitValue: true,
},
cli.BoolFlag{
Name: "share",
Patterns: []string{"--share"},
Description: "Share file",
OmitValue: true,
},
),
},
},
Expand Down
23 changes: 19 additions & 4 deletions handlers_drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ func updateHandler(ctx cli.Context) {
Name: args.String("name"),
Parents: args.StringSlice("parent"),
Mime: args.String("mime"),
Share: args.Bool("share"),
Progress: progressWriter(args.Bool("noProgress")),
ChunkSize: args.Int64("chunksize"),
})
Expand All @@ -168,7 +167,6 @@ func importHandler(ctx cli.Context) {
Out: os.Stdout,
Path: args.String("path"),
Parents: args.StringSlice("parent"),
Share: args.Bool("share"),
Progress: progressWriter(args.Bool("noProgress")),
})
checkErr(err)
Expand Down Expand Up @@ -204,7 +202,6 @@ func mkdirHandler(ctx cli.Context) {
Out: os.Stdout,
Name: args.String("name"),
Parents: args.StringSlice("parent"),
Share: args.Bool("share"),
})
checkErr(err)
}
Expand All @@ -218,7 +215,25 @@ func shareHandler(ctx cli.Context) {
Type: args.String("type"),
Email: args.String("email"),
Discoverable: args.Bool("discoverable"),
Revoke: args.Bool("revoke"),
})
checkErr(err)
}

func shareListHandler(ctx cli.Context) {
args := ctx.Args()
err := newDrive(args).ListPermissions(drive.ListPermissionsArgs{
Out: os.Stdout,
FileId: args.String("fileId"),
})
checkErr(err)
}

func shareRevokeHandler(ctx cli.Context) {
args := ctx.Args()
err := newDrive(args).RevokePermission(drive.RevokePermissionArgs{
Out: os.Stdout,
FileId: args.String("fileId"),
PermissionId: args.String("permissionId"),
})
checkErr(err)
}
Expand Down

0 comments on commit 713b062

Please sign in to comment.