-
Notifications
You must be signed in to change notification settings - Fork 905
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1bed8c8
feat: Add suspend/active user to cli
Emyrk ddf2571
Add aliases
Emyrk 77f4890
Add comment to displayUsers
Emyrk f57008c
Add comment to displayUsers
Emyrk 6989e13
Import order
Emyrk 23a8191
Fix unit test
Emyrk 02968cb
Fix linting
Emyrk 40bef92
Fix linting
Emyrk 5e80192
Fix unit test
Emyrk 53adce9
PR cleanup, better short msg
Emyrk 2ed4249
Move command outside status subgroup
Emyrk ab8e5d1
Fix table headers
Emyrk e19b3bb
UserID is now a string and allows for username too
Emyrk c36a787
rename args
Emyrk f062a23
Merge remote-tracking branch 'origin/main' into stevenmasley/user_cli…
Emyrk c228923
Fix tests
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,48 @@ | ||
package cli | ||
|
||
import "github.com/spf13/cobra" | ||
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()) | ||
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() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.User(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), | ||
IsConfirm: true, | ||
Default: "yes", | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = client.UpdateUserStatus(cmd.Context(), user.ID.String(), 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"}, | ||
"Specify a column to filter in the table.") | ||
return cmd | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Username) | ||
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.String()) | ||
require.NoError(t, err, "fetch active user") | ||
require.Equal(t, otherUser.Status, codersdk.UserStatusActive, "active user") | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of this verb is really nice here!