/
profile.go
231 lines (198 loc) · 5.65 KB
/
profile.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package cmd
import (
"context"
"fmt"
"os"
"text/tabwriter"
"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/prompts"
"github.com/manifoldco/torus-cli/ui"
"github.com/urfave/cli"
)
func init() {
profile := cli.Command{
Name: "profile",
Usage: "Manage your Torus account",
Category: "ACCOUNT",
Subcommands: []cli.Command{
{
Name: "view",
Usage: "View your profile",
Action: chain(
ensureDaemon, ensureSession, setUserEnv, profileView,
),
},
{
Name: "update",
Usage: "Update your profile",
Action: chain(
ensureDaemon, ensureSession, setUserEnv, profileEdit,
),
},
},
}
Cmds = append(Cmds, profile)
}
// profileView is used to view your account profile
func profileView(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
c := context.Background()
session, err := client.Session.Who(c)
if err != nil {
return errs.NewErrorExitError("Error fetching user details", err)
}
printProfile(session)
return nil
}
func printProfile(session *api.Session) {
w := tabwriter.NewWriter(os.Stdout, 2, 0, 1, ' ', 0)
if session.Type() == apitypes.MachineSession {
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Machine ID"), session.ID())
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Machine Token ID"), session.AuthID())
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Machine Name"), ui.FaintString(session.Username()))
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Machine State"), colorizeAccountState(session))
} else {
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Name"), session.Name())
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Username"), ui.FaintString(session.Username()))
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Email"), session.Email())
fmt.Fprintf(w, "%s:\t%s\n", ui.BoldString("Status"), colorizeAccountState(session))
}
w.Flush()
}
func colorizeAccountState(session *api.Session) string {
if session.Type() == apitypes.MachineSession {
return colorizeMachineState(session.State())
}
return colorizeUserState(session.State())
}
func colorizeUserState(state string) string {
switch state {
case "active":
return ui.ColorString(ui.Green, state)
case "unverified":
return ui.ColorString(ui.Yellow, state)
default:
return state
}
}
// profileEdit is used to update name and email for an account
func profileEdit(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
c := context.Background()
session, err := client.Session.Who(c)
if err != nil {
return errs.NewErrorExitError("Error fetching user details", err)
}
if session.Type() == apitypes.MachineSession {
return errs.NewExitError("Machines cannot update profiles")
}
ogName := session.Name()
name, err := prompts.FullName(ogName, false)
if err != nil {
return err
}
ogEmail := session.Email()
email, err := prompts.Email(ogEmail, false)
if err != nil {
return err
}
question := "Would you like to change your password"
success, err := prompts.Confirm(&question, nil, false, false)
if err != nil {
return errs.NewErrorExitError("Failed to answer question", err)
}
var newPassword string
if success {
password, err := changePassword(&c, client, session)
if err != nil {
return err
}
newPassword = password
}
// Don't perform any action if no changes occurred
if ogEmail == email && ogName == name && newPassword == "" {
fmt.Println("\nNo changes made :)")
return nil
}
// Construct the update payload
delta := apitypes.ProfileUpdate{}
mustVerify := false
if ogEmail != email {
delta.Email = email
mustVerify = true
}
if ogName != name {
delta.Name = name
}
if newPassword != "" {
delta.Password = newPassword
}
preamble := "\nYou are about to update your profile to the values entered above."
if email != ogEmail {
preamble = "\nYou will be required to re-verify your email address before taking any further actions within Torus."
}
success, err = prompts.Confirm(nil, &preamble, false, true)
if err != nil {
return errs.NewErrorExitError("Failed to retrieve confirmation", err)
}
if !success {
return errs.ErrAbort
}
// Update the profile
result, err := client.Users.Update(c, delta)
if err != nil {
return errs.NewErrorExitError("Failed to update profile.", err)
}
currentEmail := result.Email()
// If the password has changed, log the user in again
if newPassword != "" {
err = performLogin(c, client, currentEmail, newPassword, false)
if err != nil {
return err
}
}
if mustVerify {
fmt.Println("")
fmt.Println("A verification code has been sent to your new email address.")
fmt.Println("Please verify this change by entering the code below.")
fmt.Println("")
if err = askToVerify(ctx); err != nil {
return err
}
}
// Output the final session details
updatedSession, err := client.Session.Who(c)
if err != nil {
return errs.NewErrorExitError("Error fetching user details", err)
}
fmt.Println("")
printProfile(updatedSession)
return nil
}
func changePassword(c *context.Context, client *api.Client, session *api.Session) (string, error) {
// Retrieve current password value
oldLabel := "Current Password"
currentPassword, err := prompts.Password(false, &oldLabel)
if err != nil {
return "", err
}
// Test the user's current password
err = testLogin(*c, client, session.Email(), currentPassword)
if err != nil {
return "", errs.NewExitError("Invalid password.")
}
// Obtain new value for password
newLabel := "New Password"
return prompts.Password(true, &newLabel)
}