-
Notifications
You must be signed in to change notification settings - Fork 240
/
remove.go
71 lines (55 loc) · 1.45 KB
/
remove.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
package orgs
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/superfly/flyctl/api"
"github.com/superfly/flyctl/iostreams"
"github.com/superfly/flyctl/client"
"github.com/superfly/flyctl/internal/command"
)
func newRemove() *cobra.Command {
const (
long = `Remove a user from an organization. User must have accepted a previous
invitation to join (if not, see orgs revoke).
`
short = "Remove a user from an organization"
usage = "remove [slug] [email]"
)
cmd := command.New(usage, short, long, runRemove,
command.RequireSession)
cmd.Args = cobra.MaximumNArgs(2)
return cmd
}
func runRemove(ctx context.Context) error {
client := client.FromContext(ctx).API()
selectedOrg, err := OrgFromFirstArgOrSelect(ctx, api.AdminOnly)
if err != nil {
return nil
}
org, err := client.GetDetailedOrganizationBySlug(ctx, selectedOrg.Slug)
if err != nil {
return nil
}
email, err := emailFromSecondArgOrPrompt(ctx)
if err != nil {
return nil
}
var id string
for _, m := range org.Members.Edges {
if m.Node.Email == email {
id = m.Node.ID
break
}
}
if id == "" {
return errors.New("user not found")
}
if _, _, err := client.DeleteOrganizationMembership(ctx, org.ID, id); err != nil {
return fmt.Errorf("failed removing user %s from %s: %w", email, org.Name, err)
}
io := iostreams.FromContext(ctx)
fmt.Fprintf(io.Out, "successfuly removed user %s from %s\n", email, org.Name)
return nil
}