- Notifications
You must be signed in to change notification settings - Fork928
feat: Add suspend/active user to cli#1422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from11 commits
1bed8c8
ddf2571
77f4890
f57008c
6989e13
23a8191
02968cb
40bef92
5e80192
53adce9
2ed4249
ab8e5d1
e19b3bb
c36a787
f062a23
c228923
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,48 @@ | ||
package cli | ||
import ( | ||
"time" | ||
"github.com/jedib0t/go-pretty/v6/table" | ||
"github.com/spf13/cobra" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
func users() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Short: "Create, remove, and list users", | ||
Use: "users", | ||
} | ||
cmd.AddCommand( | ||
userCreate(), | ||
userList(), | ||
createUserStatusCommand(codersdk.UserStatusActive), | ||
createUserStatusCommand(codersdk.UserStatusSuspended), | ||
) | ||
return cmd | ||
} | ||
// displayUsers will return a table displaying all users passed in. | ||
// filterColumns must be a subset of the user fields and will determine which | ||
// columns to display | ||
func displayUsers(filterColumns []string, users ...codersdk.User) string { | ||
tableWriter := cliui.Table() | ||
header := table.Row{"ID", "Username", "Email", "Created At", "Status"} | ||
tableWriter.AppendHeader(header) | ||
tableWriter.SetColumnConfigs(cliui.FilterTableColumns(header, filterColumns)) | ||
tableWriter.SortBy([]table.SortBy{{ | ||
Name: "Username", | ||
}}) | ||
for _, user := range users { | ||
tableWriter.AppendRow(table.Row{ | ||
user.ID.String(), | ||
user.Username, | ||
user.Email, | ||
user.CreatedAt.Format(time.Stamp), | ||
user.Status, | ||
}) | ||
} | ||
return tableWriter.Render() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package cli | ||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
"github.com/coder/coder/cli/cliui" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
// createUserStatusCommand sets a user status. | ||
func createUserStatusCommand(sdkStatus codersdk.UserStatus) *cobra.Command { | ||
var verb string | ||
var aliases []string | ||
var short string | ||
switch sdkStatus { | ||
case codersdk.UserStatusActive: | ||
verb = "activate" | ||
aliases = []string{"active"} | ||
short = "Update a user's status to 'active'. Active users can fully interact with the platform" | ||
case codersdk.UserStatusSuspended: | ||
verb = "suspend" | ||
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)) | ||
} | ||
var ( | ||
columns []string | ||
) | ||
cmd := &cobra.Command{ | ||
Use: fmt.Sprintf("%s <username|user_id>", verb), | ||
Short: short, | ||
Args: cobra.ExactArgs(1), | ||
Aliases: aliases, | ||
Example: fmt.Sprintf("coder users %s example_user", verb), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
identifier := args[0] | ||
if identifier == "" { | ||
return xerrors.Errorf("user identifier cannot be an empty string") | ||
} | ||
user, err := client.UserByIdentifier(cmd.Context(), identifier) | ||
if err != nil { | ||
return xerrors.Errorf("fetch user: %w", err) | ||
} | ||
// Display the user | ||
_, _ = fmt.Fprintln(cmd.OutOrStdout(), displayUsers(columns, user)) | ||
// User status is already set to this | ||
if user.Status == sdkStatus { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "User status is already %q\n", sdkStatus) | ||
return nil | ||
} | ||
// Prompt to confirm the action | ||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{ | ||
Text: fmt.Sprintf("Are you sure you want to %s this user?", verb), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. The use of this verb is really nice here! | ||
IsConfirm: true, | ||
Default: "yes", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
_, err = client.UpdateUserStatus(cmd.Context(), user.ID, sdkStatus) | ||
if err != nil { | ||
return xerrors.Errorf("%s user: %w", verb, err) | ||
} | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"Username", "Email", "Created At", "Status"}, | ||
Emyrk marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
"Specify a column to filter in the table.") | ||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cli_test | ||
import ( | ||
"bytes" | ||
"context" | ||
"testing" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/cli/clitest" | ||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
func TestUserStatus(t *testing.T) { | ||
t.Parallel() | ||
client := coderdtest.New(t, nil) | ||
admin := coderdtest.CreateFirstUser(t, client) | ||
other := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID) | ||
otherUser, err := other.User(context.Background(), codersdk.Me) | ||
require.NoError(t, err, "fetch user") | ||
//nolint:paralleltest | ||
t.Run("StatusSelf", func(t *testing.T) { | ||
cmd, root := clitest.New(t, "users", "suspend", "me") | ||
clitest.SetupConfig(t, client, root) | ||
// Yes to the prompt | ||
cmd.SetIn(bytes.NewReader([]byte("yes\n"))) | ||
err := cmd.Execute() | ||
// Expect an error, as you cannot suspend yourself | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "cannot suspend yourself") | ||
}) | ||
//nolint:paralleltest | ||
t.Run("StatusOther", func(t *testing.T) { | ||
require.Equal(t, otherUser.Status, codersdk.UserStatusActive, "start as active") | ||
cmd, root := clitest.New(t, "users", "suspend", otherUser.Username) | ||
clitest.SetupConfig(t, client, root) | ||
// Yes to the prompt | ||
cmd.SetIn(bytes.NewReader([]byte("yes\n"))) | ||
err := cmd.Execute() | ||
require.NoError(t, err, "suspend user") | ||
// Check the user status | ||
otherUser, err = client.User(context.Background(), otherUser.ID) | ||
require.NoError(t, err, "fetch suspended user") | ||
require.Equal(t, otherUser.Status, codersdk.UserStatusSuspended, "suspended user") | ||
// Set back to active. Try using a uuid as well | ||
cmd, root = clitest.New(t, "users", "activate", otherUser.ID.String()) | ||
clitest.SetupConfig(t, client, root) | ||
// Yes to the prompt | ||
cmd.SetIn(bytes.NewReader([]byte("yes\n"))) | ||
err = cmd.Execute() | ||
require.NoError(t, err, "suspend user") | ||
// Check the user status | ||
otherUser, err = client.User(context.Background(), otherUser.ID) | ||
require.NoError(t, err, "fetch active user") | ||
require.Equal(t, otherUser.Status, codersdk.UserStatusActive, "active user") | ||
}) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.