Skip to content

Commit

Permalink
share: provide option to not notify
Browse files Browse the repository at this point in the history
+ Set whether not to notify the user on successful sharing.
+ Fixed bugs + arg mismatches, reorganized permissions.
  • Loading branch information
Emmanuel Odeke committed Jan 31, 2015
1 parent 18e7790 commit 2e3c1bb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 23 deletions.
18 changes: 13 additions & 5 deletions cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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())
}

Expand Down
6 changes: 3 additions & 3 deletions src/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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." +
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
26 changes: 18 additions & 8 deletions src/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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
}
Expand Down
49 changes: 42 additions & 7 deletions src/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,28 @@ const (
Commenter
)

const (
NoopOnShare = 1 << iota
Notify
)

type shareChange struct {
emailMessage string
emails []string
role Role
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 {
Expand Down Expand Up @@ -79,29 +94,29 @@ 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
}
}

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
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -241,13 +273,16 @@ func (c *Commands) share(revoke bool) (err error) {
}
}

notify := (c.opts.TypeMask & Notify) != 0

change := shareChange{
accountType: accountType,
emailMessage: emailMessage,
emails: emails,
files: files,
revoke: revoke,
role: role,
notify: notify,
}

return c.playShareChanges(&change)
Expand Down

0 comments on commit 2e3c1bb

Please sign in to comment.