/
invites_accept.go
95 lines (80 loc) · 2.06 KB
/
invites_accept.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
package cmd
import (
"context"
"fmt"
"github.com/urfave/cli"
"github.com/manifoldco/torus-cli/api"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/prompts"
"github.com/manifoldco/torus-cli/validate"
)
const acceptInviteFailed = "Could not accept invitation to org, please try again."
func invitesAccept(ctx *cli.Context) error {
args := ctx.Args()
if len(args) < 2 {
var text string
if len(args) < 1 {
text = "Missing email and code"
} else {
text = "Missing code"
}
return errs.NewUsageExitError(text, ctx)
}
cfg, err := config.LoadConfig()
if err != nil {
return errs.NewExitError(envCreateFailed)
}
client := api.NewClient(cfg)
c := context.Background()
_, err = client.Session.Get(c)
if err != nil {
_, value, err := prompts.SelectAcceptAction()
if err != nil {
if err == errs.ErrAbort {
return err
}
return errs.NewExitError(acceptInviteFailed)
}
// Become logged in either through signup or login
switch value {
case "Login":
fmt.Println("")
err = login(ctx)
if err != nil {
return err
}
case "Signup":
fmt.Println("")
err = signup(ctx, true)
if err != nil {
return err
}
default:
return errs.NewExitError(acceptInviteFailed)
}
fmt.Println("")
}
email := args[0]
code := args[1]
err = validate.InviteCode(code)
if err != nil {
return err
}
invite, err := client.OrgInvites.Associate(c, ctx.String("org"), email, code)
if err != nil || invite == nil {
return errs.NewExitError(acceptInviteFailed)
}
err = generateKeypairsForOrg(c, ctx, client, invite.Body.OrgID, false)
if err != nil {
// We'd rather they generate keypairs through accept again, so generic err
return errs.NewExitError(acceptInviteFailed)
}
err = client.OrgInvites.Accept(c, ctx.String("org"), email, code)
if err != nil {
return errs.NewExitError(acceptInviteFailed)
}
fmt.Println("You have accepted the invitation.")
fmt.Println("\nYou will be added to the org once the administrator has approved your invite.")
return nil
}