- 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
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
16 commits Select commitHold shift + click to select a range
1bed8c8
feat: Add suspend/active user to cli
Emyrkddf2571
Add aliases
Emyrk77f4890
Add comment to displayUsers
Emyrkf57008c
Add comment to displayUsers
Emyrk6989e13
Import order
Emyrk23a8191
Fix unit test
Emyrk02968cb
Fix linting
Emyrk40bef92
Fix linting
Emyrk5e80192
Fix unit test
Emyrk53adce9
PR cleanup, better short msg
Emyrk2ed4249
Move command outside status subgroup
Emyrkab8e5d1
Fix table headers
Emyrke19b3bb
UserID is now a string and allows for username too
Emyrkc36a787
rename args
Emyrkf062a23
Merge remote-tracking branch 'origin/main' into stevenmasley/user_cli…
Emyrkc228923
Fix tests
EmyrkFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
21 changes: 2 additions & 19 deletionscli/userlist.go
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
40 changes: 38 additions & 2 deletionscli/users.go
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 ( | ||
"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() | ||
} |
85 changes: 85 additions & 0 deletionscli/userstatus.go
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), | ||
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.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 | ||
} |
64 changes: 64 additions & 0 deletionscli/userstatus_test.go
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") | ||
}) | ||
} |
2 changes: 1 addition & 1 deletioncoderd/autobuild/executor/lifecycle_executor_test.go
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
5 changes: 4 additions & 1 deletioncoderd/coderd.go
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
4 changes: 2 additions & 2 deletionscoderd/coderdtest/coderdtest.go
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
12 changes: 6 additions & 6 deletionscoderd/gitsshkey_test.go
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
2 changes: 1 addition & 1 deletioncoderd/roles_test.go
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.
Uh oh!
There was an error while loading.Please reload this page.
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.