From 2e3c1bb98fa29b87d2003cad7758805fb261eb99 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sat, 31 Jan 2015 00:31:55 -0700 Subject: [PATCH] share: provide option to not notify + Set whether not to notify the user on successful sharing. + Fixed bugs + arg mismatches, reorganized permissions. --- cmd/drive/main.go | 18 ++++++++++++----- src/help.go | 6 +++--- src/remote.go | 26 +++++++++++++++++-------- src/share.go | 49 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/cmd/drive/main.go b/cmd/drive/main.go index c7cdd270..8072f8b7 100644 --- a/cmd/drive/main.go +++ b/cmd/drive/main.go @@ -591,13 +591,15 @@ type shareCmd struct { message *string role *string accountType *string + notify *bool } func (cmd *shareCmd) Flags(fs *flag.FlagSet) *flag.FlagSet { cmd.emails = fs.String("emails", "", "emails to share the file to") cmd.message = fs.String("message", "", "message to send receipients") - cmd.role = fs.String("role", "", "role to set to receipients of share") - cmd.accountType = fs.String("type", "", "scope of accounts to share files with") + cmd.role = fs.String("role", "", "role to set to receipients of share. Possible values: "+drive.DescRoles) + cmd.accountType = fs.String("type", "", "scope of accounts to share files with. Possible values: "+drive.DescAccountTypes) + cmd.notify = fs.Bool("notify", true, "toggle whether to notify receipients about share") return fs } @@ -611,10 +613,16 @@ func (cmd *shareCmd) Run(args []string) { "accountType": uniqOrderedStr(nonEmptyStrings(strings.Split(*cmd.accountType, ","))), } + mask := drive.NoopOnShare + if *cmd.notify { + mask = drive.Notify + } + exitWithError(drive.New(context, &drive.Options{ - Meta: &meta, - Path: path, - Sources: sources, + Meta: &meta, + Path: path, + Sources: sources, + TypeMask: mask, }).Share()) } diff --git a/src/help.go b/src/help.go index 970278ec..067b4695 100644 --- a/src/help.go +++ b/src/help.go @@ -62,7 +62,7 @@ const ( DescUntrash = "restores files from trash to their original locations" DescUnpublish = "revokes public access to a file" DescVersion = "prints the version" - DescAccountType = "\n\t* anyone.\n\t* user.\n\t* domain.\n\t* group" + DescAccountTypes = "\n\t* anyone.\n\t* user.\n\t* domain.\n\t* group" DescRoles = "\n\t* owner.\n\t* reader.\n\t* writer.\n\t* commenter." DescIgnoreChecksum = "avoids computation of checksums as a final check." + "\nUse cases may include:\n\t* when you are low on bandwidth e.g SSHFS." + @@ -120,7 +120,7 @@ var docMap = map[string][]string{ DescShare, "Accepts multiple paths", "Specify the emails to share with as well as the message to send them on notification", "Accepted values for:\n+ accountType: ", - DescAccountType, "\n+ roles:", DescRoles, + DescAccountTypes, "\n+ roles:", DescRoles, }, StatKey: []string{ DescStat, "provides detailed information about a remote file", @@ -135,7 +135,7 @@ var docMap = map[string][]string{ }, UnshareKey: []string{ DescUnshare, "Accepts multiple paths", - "Accepted values for accountTypes::", DescAccountType, + "Accepted values for accountTypes::", DescAccountTypes, }, UntrashKey: []string{ DescUntrash, "takes remote files out of the trash", diff --git a/src/remote.go b/src/remote.go index 04fac7f8..4e12de4a 100644 --- a/src/remote.go +++ b/src/remote.go @@ -280,16 +280,21 @@ func (r *Remote) listPermissions(id string) ([]*drive.Permission, error) { return res.Items, nil } -func (r *Remote) insertPermissions(id, value, emailMessage string, role Role, accountType AccountType) (*drive.Permission, error) { - perm := &drive.Permission{Role: role.String(), Type: accountType.String()} - if value != "" { - perm.Value = value +func (r *Remote) insertPermissions(permInfo *permission) (*drive.Permission, error) { + perm := &drive.Permission{ + Role: permInfo.role.String(), + Type: permInfo.accountType.String(), } - req := r.service.Permissions.Insert(id, perm) - if emailMessage != "" { - req = req.EmailMessage(emailMessage) + if permInfo.value != "" { + perm.Value = permInfo.value } + req := r.service.Permissions.Insert(permInfo.fileId, perm) + + if permInfo.message != "" { + req = req.EmailMessage(permInfo.message) + } + req = req.SendNotificationEmails(permInfo.notify) return req.Do() } @@ -302,7 +307,12 @@ func (r *Remote) Unpublish(id string) error { } func (r *Remote) Publish(id string) (string, error) { - _, err := r.insertPermissions(id, "", "", Reader, Anyone) + _, err := r.insertPermissions(&permission{ + fileId: id, + value: "", + role: Reader, + accountType: Anyone, + }) if err != nil { return "", err } diff --git a/src/share.go b/src/share.go index 4f423e0d..8bc34a45 100644 --- a/src/share.go +++ b/src/share.go @@ -40,6 +40,11 @@ const ( Commenter ) +const ( + NoopOnShare = 1 << iota + Notify +) + type shareChange struct { emailMessage string emails []string @@ -47,6 +52,16 @@ type shareChange struct { accountType AccountType files []*File revoke bool + notify bool +} + +type permission struct { + fileId string + value string + message string + role Role + accountType AccountType + notify bool } func (r *Role) String() string { @@ -79,14 +94,14 @@ func (a *AccountType) String() string { func stringToRole() func(string) Role { roleMap := make(map[string]Role) - roles := []Role{UnknownRole, Anyone, User, Domain, Group} + roles := []Role{UnknownRole, Owner, Reader, Writer, Commenter} for _, role := range roles { roleMap[role.String()] = role } return func(s string) Role { r, ok := roleMap[strings.ToLower(s)] if !ok { - return UnknownRole + return Reader } return r } @@ -94,14 +109,14 @@ func stringToRole() func(string) Role { func stringToAccountType() func(string) AccountType { accountMap := make(map[string]AccountType) - accounts := []AccountType{UnknownAccountType, Owner, Reader, Writer, Commenter} + accounts := []AccountType{UnknownAccountType, Anyone, User, Domain, Group} for _, account := range accounts { accountMap[account.String()] = account } return func(s string) AccountType { a, ok := accountMap[strings.ToLower(s)] if !ok { - return UnknownAccountType + return User } return a } @@ -158,7 +173,8 @@ func showPromptShareChanges(change *shareChange) bool { return false } if change.revoke { - fmt.Printf("Revoke access for accountType: \033[92m%s\033[00m for file(s):\n", change.accountType.String()) + fmt.Printf("Revoke access for accountType: \033[92m%s\033[00m for file(s):\n", + change.accountType.String()) for _, file := range change.files { fmt.Println("+ ", file.Name) } @@ -170,7 +186,10 @@ func showPromptShareChanges(change *shareChange) bool { return false } - fmt.Println("Message:\n\t", change.emailMessage) + if change.notify { + fmt.Println("Message:\n\t", change.emailMessage) + } + fmt.Println("Receipients:") for _, email := range change.emails { fmt.Printf("\t\033[92m+\033[00m %s\n", email) @@ -190,6 +209,7 @@ func (c *Commands) playShareChanges(change *shareChange) error { if !showPromptShareChanges(change) { return nil } + for _, file := range change.files { if change.revoke { if err := c.rem.deletePermissions(file.Id, change.accountType); err != nil { @@ -198,7 +218,15 @@ func (c *Commands) playShareChanges(change *shareChange) error { } for _, email := range change.emails { - _, err := c.rem.insertPermissions(file.Id, email, change.emailMessage, change.role, change.accountType) + perm := permission{ + fileId: file.Id, + value: email, + message: change.emailMessage, + notify: change.notify, + role: change.role, + accountType: change.accountType, + } + _, err := c.rem.insertPermissions(&perm) if err != nil { return err } @@ -215,7 +243,11 @@ func (c *Commands) share(revoke bool) (err error) { var emails []string var emailMessage string + // Setup the defaults + role = Reader + accountType = User meta := *c.opts.Meta + if meta != nil { emailList, eOk := meta["emails"] if eOk { @@ -241,6 +273,8 @@ func (c *Commands) share(revoke bool) (err error) { } } + notify := (c.opts.TypeMask & Notify) != 0 + change := shareChange{ accountType: accountType, emailMessage: emailMessage, @@ -248,6 +282,7 @@ func (c *Commands) share(revoke bool) (err error) { files: files, revoke: revoke, role: role, + notify: notify, } return c.playShareChanges(&change)