forked from keybase/client
/
cmd_team_add_member.go
156 lines (128 loc) · 3.64 KB
/
cmd_team_add_member.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2017 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package client
import (
"context"
"errors"
"github.com/keybase/cli"
"github.com/keybase/client/go/libcmdline"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
)
type CmdTeamAddMember struct {
libkb.Contextified
Team string
Email string
Username string
Role keybase1.TeamRole
SkipChatNotification bool
}
func newCmdTeamAddMember(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
return cli.Command{
Name: "add-member",
ArgumentHelp: "<team name>",
Usage: "Add a user to a team.",
Action: func(c *cli.Context) {
cmd := NewCmdTeamAddMemberRunner(g)
cl.ChooseCommand(cmd, "add-member", c)
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "u, user",
Usage: "username",
},
cli.StringFlag{
Name: "e, email",
Usage: "email address to invite",
},
cli.StringFlag{
Name: "r, role",
Usage: "team role (owner, admin, writer, reader) [required]",
},
},
Description: teamAddMemberDoc,
}
}
func NewCmdTeamAddMemberRunner(g *libkb.GlobalContext) *CmdTeamAddMember {
return &CmdTeamAddMember{Contextified: libkb.NewContextified(g)}
}
func (c *CmdTeamAddMember) ParseArgv(ctx *cli.Context) error {
var err error
c.Team, err = ParseOneTeamName(ctx)
if err != nil {
return err
}
c.Role, err = ParseRole(ctx)
if err != nil {
return err
}
c.Email = ctx.String("email")
if len(c.Email) > 0 {
if !libkb.CheckEmail.F(c.Email) {
return errors.New("invalid email address")
}
return nil
}
c.Username, err = ParseUser(ctx)
if err != nil {
return err
}
return nil
}
func (c *CmdTeamAddMember) Run() error {
cli, err := GetTeamsClient(c.G())
if err != nil {
return err
}
arg := keybase1.TeamAddMemberArg{
Name: c.Team,
Email: c.Email,
Username: c.Username,
Role: c.Role,
SendChatNotification: !c.SkipChatNotification,
}
res, err := cli.TeamAddMember(context.Background(), arg)
if err != nil {
return err
}
dui := c.G().UI.GetDumbOutputUI()
if !res.Invited {
// TeamAddMember resulted in the user added to the team
if res.ChatSent {
dui.Printf("Success! A keybase chat message has been sent to %s.\n", res.User.Username)
} else {
dui.Printf("Success! %s added to team.\n", res.User.Username)
}
return nil
}
// TeamAddMember resulted in the user invited to the team
if c.Email != "" {
// email invitation
dui.Printf("Pending! Email sent to %s with signup instructions. When they join you will be notified.\n", c.Email)
return nil
}
if res.User != nil {
// user without keys or without puk
dui.Printf("Pending! Keybase stored a team invitation for %s. When they open the Keybase app, their account will be upgraded and you will be notified.\n", res.User.Username)
} else {
// "sharing before signup" user
dui.Printf("Pending! Keybase stored a team invitation for %s. When they join Keybase you will be notified.\n", c.Username)
}
return nil
}
func (c *CmdTeamAddMember) GetUsage() libkb.Usage {
return libkb.Usage{
Config: true,
API: true,
KbKeyring: true,
}
}
const teamAddMemberDoc = `"keybase team add-member" allows you to add users to a team.
EXAMPLES:
Add an existing keybase user:
keybase team add-member acme --user=alice --role=writer
Add a user via social assertion:
keybase team add-member acme --user=alice@twitter --role=writer
Add a user via email:
keybase team add-member acme --email=alice@mail.com --role=reader
`