From c468039d941bd3f4cde35813617e32e42a357bd9 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 30 Jul 2019 17:36:55 -0400 Subject: [PATCH] Add functions for adding groups of Admins, Owners, Readers, and Writers to teams --- team.go | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- types.go | 4 +++ 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/team.go b/team.go index fd357d3..89a970b 100644 --- a/team.go +++ b/team.go @@ -22,7 +22,7 @@ func teamAPIOut(keybasePath string, w TeamAPI) (TeamAPI, error) { return r, nil } -// AddUser adds members to a team by username +// AddUser adds a member to a team by username func (t Team) AddUser(user, role string) (TeamAPI, error) { m := TeamAPI{ Params: &tParams{}, @@ -37,7 +37,87 @@ func (t Team) AddUser(user, role string) (TeamAPI, error) { } r, err := teamAPIOut(t.keybase.Path, m) - if err == nil { + if err == nil && r.Error == nil { + r, err = t.MemberList() + } + return r, err +} + +// AddReaders adds members to a team by username, and sets their roles to Reader +func (t Team) AddReaders(users ...string) (TeamAPI, error) { + m := TeamAPI{ + Params: &tParams{}, + } + m.Method = "add-members" + m.Params.Options.Team = t.Name + addUsers := []usernames{} + for _, u := range users { + addUsers = append(addUsers, usernames{Username: u, Role: "reader"}) + } + m.Params.Options.Usernames = addUsers + + r, err := teamAPIOut(t.keybase.Path, m) + if err == nil && r.Error == nil { + r, err = t.MemberList() + } + return r, err +} + +// AddWriters adds members to a team by username, and sets their roles to Writer +func (t Team) AddWriters(users ...string) (TeamAPI, error) { + m := TeamAPI{ + Params: &tParams{}, + } + m.Method = "add-members" + m.Params.Options.Team = t.Name + addUsers := []usernames{} + for _, u := range users { + addUsers = append(addUsers, usernames{Username: u, Role: "writer"}) + } + m.Params.Options.Usernames = addUsers + + r, err := teamAPIOut(t.keybase.Path, m) + if err == nil && r.Error == nil { + r, err = t.MemberList() + } + return r, err +} + +// AddAdmins adds members to a team by username, and sets their roles to Writer +func (t Team) AddAdmins(users ...string) (TeamAPI, error) { + m := TeamAPI{ + Params: &tParams{}, + } + m.Method = "add-members" + m.Params.Options.Team = t.Name + addUsers := []usernames{} + for _, u := range users { + addUsers = append(addUsers, usernames{Username: u, Role: "admin"}) + } + m.Params.Options.Usernames = addUsers + + r, err := teamAPIOut(t.keybase.Path, m) + if err == nil && r.Error == nil { + r, err = t.MemberList() + } + return r, err +} + +// AddOwners adds members to a team by username, and sets their roles to Writer +func (t Team) AddOwners(users ...string) (TeamAPI, error) { + m := TeamAPI{ + Params: &tParams{}, + } + m.Method = "add-members" + m.Params.Options.Team = t.Name + addUsers := []usernames{} + for _, u := range users { + addUsers = append(addUsers, usernames{Username: u, Role: "owner"}) + } + m.Params.Options.Usernames = addUsers + + r, err := teamAPIOut(t.keybase.Path, m) + if err == nil && r.Error == nil { r, err = t.MemberList() } return r, err diff --git a/types.go b/types.go index 85fab32..697dddb 100644 --- a/types.go +++ b/types.go @@ -429,7 +429,11 @@ type Team struct { } type team interface { + AddAdmins(users ...string) (TeamAPI, error) + AddOwners(users ...string) (TeamAPI, error) + AddReaders(users ...string) (TeamAPI, error) AddUser(user, role string) (TeamAPI, error) + AddWriters(users ...string) (TeamAPI, error) CreateSubteam(name string) (TeamAPI, error) MemberList() (TeamAPI, error) }