-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathexp_update_user_email.go
More file actions
81 lines (70 loc) · 1.96 KB
/
Copy pathexp_update_user_email.go
File metadata and controls
81 lines (70 loc) · 1.96 KB
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
package cli
import (
"fmt"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/serpent"
)
func (r *RootCmd) updateUserEmail() *serpent.Command {
var (
oldEmail string
newEmail string
)
cmd := &serpent.Command{
Use: "update-user-email",
Short: "Update a user's email address (break-glass; experimental)",
Hidden: true,
Options: serpent.OptionSet{
{
Flag: "old-email",
Description: "Current email address of the user to update.",
Required: true,
Value: serpent.StringOf(&oldEmail),
},
{
Flag: "new-email",
Description: "New email address to assign to the user.",
Required: true,
Value: serpent.StringOf(&newEmail),
},
cliui.SkipPromptOption(),
},
Handler: func(inv *serpent.Invocation) error {
if oldEmail == "" {
return xerrors.Errorf("--old-email must not be blank")
}
if newEmail == "" {
return xerrors.Errorf("--new-email must not be blank")
}
client, err := r.InitClient(inv)
if err != nil {
return err
}
_, _ = fmt.Fprintf(inv.Stdout,
"This will update the email address for the account currently using %q to %q.\n"+
"All Coder sessions and API tokens for that user will be revoked.\n"+
"If the user logs in with an external identity provider, it may overwrite the new email when the user next signs in.\n",
oldEmail, newEmail,
)
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: "Confirm email update?",
IsConfirm: true,
Default: cliui.ConfirmNo,
})
if err != nil {
return err
}
err = client.UpdateUserEmail(inv.Context(), codersdk.UpdateUserEmailRequest{
OldEmail: oldEmail,
NewEmail: newEmail,
})
if err != nil {
return xerrors.Errorf("update user email: %w", err)
}
_, _ = fmt.Fprintf(inv.Stdout, "Updated user email from %s to %s.\n", oldEmail, newEmail)
return nil
},
}
return cmd
}