forked from manifoldco/torus-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signup.go
166 lines (140 loc) · 3.76 KB
/
signup.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
157
158
159
160
161
162
163
164
165
166
package cmd
import (
"context"
"fmt"
"strings"
"github.com/manifoldco/torus-cli/api"
"github.com/manifoldco/torus-cli/apitypes"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/promptui"
"github.com/urfave/cli"
)
func init() {
signup := cli.Command{
Name: "signup",
Usage: "Create a new Torus account",
Category: "ACCOUNT",
Action: chain(ensureDaemon, signupCmd),
}
Cmds = append(Cmds, signup)
}
func signupCmd(ctx *cli.Context) error {
return signup(ctx, false)
}
// signup can be ran as a sub-command when an account is needed prior to running
// a particular action. the subCommand boolean signifies it is running as such
// and not as a generic signup
func signup(ctx *cli.Context, subCommand bool) error {
args := ctx.Args()
// Arguments are only used as a subcommand
if subCommand && (len(args) > 0 && len(args) != 2) {
var text string
if len(args) > 2 {
text = "Too many arguments supplied."
} else {
text = "Too few arguments supplied."
}
return errs.NewUsageExitError(text, ctx)
}
fmt.Println("By completing sign up, you agree to our terms of use (found at https://torus.sh/terms)\nand our privacy policy (found at https://torus.sh/privacy)")
fmt.Println("")
name, err := FullNamePrompt("")
if err != nil {
return err
}
username, err := UsernamePrompt("")
if err != nil {
return err
}
defaultEmail := ""
defaultInvite := ""
if len(args) == 2 {
defaultEmail = args[0]
defaultInvite = args[1]
}
email, err := EmailPrompt(defaultEmail)
if err != nil {
return err
}
var inviteCode string
if subCommand {
inviteCode, err = InviteCodePrompt(defaultInvite)
if err != nil {
return err
}
}
password, err := PasswordPrompt(true, nil)
if err != nil {
return err
}
// Whether we should enable tool hints
label := "Enable hints"
preamble := "Would you like to enable hints on how to use Torus?\nThey can be disabled at any time using `torus prefs`."
enableHints := ConfirmDialogue(ctx, &label, &preamble, "y", false)
prefValue := "true"
if enableHints != nil {
// Abort if they interrupt
if enableHints == promptui.ErrInterrupt {
return enableHints
}
prefValue = "false"
}
// Store the preference value
prefErr := setPrefByName("core.hints", prefValue)
// Alert the user we failed to disable them, but continue with signup
if prefErr != nil {
fmt.Printf("Failed to set core.hints. Run `torus prefs set core.hints %s` to try again.\n", prefValue)
}
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
signup := apitypes.Signup{
Name: name,
Username: username,
Passphrase: password,
Email: email,
InviteCode: inviteCode,
OrgName: ctx.String("org"),
OrgInvite: subCommand,
}
c := context.Background()
fmt.Println("")
user, err := client.Users.Signup(c, &signup, &progress)
if err != nil {
if strings.Contains(err.Error(), "resource exists") {
return errs.NewExitError("Username or email address in use.")
}
return errs.NewExitError("Signup failed, please try again.")
}
// Log the user in
err = performLogin(c, client, user.Body.Email, password, true)
if err != nil {
return err
}
// Generate keypairs, look up the user's org
err = generateKeypairsForOrg(c, ctx, client, nil, true)
if err != nil {
return err
}
fmt.Println("")
fmt.Println("Your account has been created!")
if !subCommand {
fmt.Println("")
fmt.Println("We have emailed you a verification code.")
fmt.Println("Please verify your email address by entering the code below.")
fmt.Println("")
code, err := VerificationPrompt()
if err != nil {
return err
}
fmt.Println("")
err = verifyEmail(ctx, &code, true)
if err != nil {
return err
}
}
return nil
}