-
Notifications
You must be signed in to change notification settings - Fork 704
/
userstatus.go
108 lines (95 loc) · 2.95 KB
/
userstatus.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
package cli
import (
"fmt"
"strings"
"golang.org/x/xerrors"
"github.com/coder/pretty"
"github.com/coder/coder/v2/cli/clibase"
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
)
// createUserStatusCommand sets a user status.
func (r *RootCmd) createUserStatusCommand(sdkStatus codersdk.UserStatus) *clibase.Cmd {
var verb string
var pastVerb string
var aliases []string
var short string
switch sdkStatus {
case codersdk.UserStatusActive:
verb = "activate"
pastVerb = "activated"
aliases = []string{"active"}
short = "Update a user's status to 'active'. Active users can fully interact with the platform"
case codersdk.UserStatusSuspended:
verb = "suspend"
pastVerb = "suspended"
aliases = []string{"rm", "delete"}
short = "Update a user's status to 'suspended'. A suspended user cannot log into the platform"
default:
panic(fmt.Sprintf("%s is not supported", sdkStatus))
}
client := new(codersdk.Client)
var columns []string
cmd := &clibase.Cmd{
Use: fmt.Sprintf("%s <username|user_id>", verb),
Short: short,
Aliases: aliases,
Long: formatExamples(
example{
Command: fmt.Sprintf("coder users %s example_user", verb),
},
),
Middleware: clibase.Chain(
clibase.RequireNArgs(1),
r.InitClient(client),
),
Handler: func(inv *clibase.Invocation) error {
identifier := inv.Args[0]
if identifier == "" {
return xerrors.Errorf("user identifier cannot be an empty string")
}
user, err := client.User(inv.Context(), identifier)
if err != nil {
return xerrors.Errorf("fetch user: %w", err)
}
// Display the user. This uses cliui.DisplayTable directly instead
// of cliui.NewOutputFormatter because we prompt immediately
// afterwards.
table, err := cliui.DisplayTable([]codersdk.User{user}, "", columns)
if err != nil {
return xerrors.Errorf("render user table: %w", err)
}
_, _ = fmt.Fprintln(inv.Stdout, table)
// User status is already set to this
if user.Status == sdkStatus {
_, _ = fmt.Fprintf(inv.Stdout, "User status is already %q\n", sdkStatus)
return nil
}
// Prompt to confirm the action
_, err = cliui.Prompt(inv, cliui.PromptOptions{
Text: fmt.Sprintf("Are you sure you want to %s this user?", verb),
IsConfirm: true,
Default: cliui.ConfirmYes,
})
if err != nil {
return err
}
_, err = client.UpdateUserStatus(inv.Context(), user.ID.String(), sdkStatus)
if err != nil {
return xerrors.Errorf("%s user: %w", verb, err)
}
_, _ = fmt.Fprintf(inv.Stdout, "\nUser %s has been %s!\n", pretty.Sprint(cliui.DefaultStyles.Keyword, user.Username), pastVerb)
return nil
},
}
cmd.Options = clibase.OptionSet{
{
Flag: "column",
FlagShorthand: "c",
Description: "Specify a column to filter in the table.",
Default: strings.Join([]string{"username", "email", "created_at", "status"}, ","),
Value: clibase.StringArrayOf(&columns),
},
}
return cmd
}