Skip to content

Commit d3a0578

Browse files
authored
feat: Allow regen-ssh and fetching a single user from the cli (#1619)
* feat: Allow regen-ssh and fetching a single user from the cli
1 parent 363b16a commit d3a0578

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

cli/publickey.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import (
1111
)
1212

1313
func publickey() *cobra.Command {
14-
return &cobra.Command{
14+
var (
15+
reset bool
16+
)
17+
18+
cmd := &cobra.Command{
1519
Use: "publickey",
1620
Aliases: []string{"pubkey"},
1721
Short: "Output your public key for Git operations",
@@ -21,6 +25,25 @@ func publickey() *cobra.Command {
2125
return xerrors.Errorf("create codersdk client: %w", err)
2226
}
2327

28+
if reset {
29+
// Confirm prompt if using --reset. We don't want to accidentally
30+
// reset our public key.
31+
_, err := cliui.Prompt(cmd, cliui.PromptOptions{
32+
Text: "Confirm regenerate a new sshkey for your workspaces? This will require updating the key " +
33+
"on any services it is registered with. This action cannot be reverted.",
34+
IsConfirm: true,
35+
})
36+
if err != nil {
37+
return err
38+
}
39+
40+
// Reset the public key, let the retrieve re-read it.
41+
_, err = client.RegenerateGitSSHKey(cmd.Context(), codersdk.Me)
42+
if err != nil {
43+
return err
44+
}
45+
}
46+
2447
key, err := client.GitSSHKey(cmd.Context(), codersdk.Me)
2548
if err != nil {
2649
return xerrors.Errorf("create codersdk client: %w", err)
@@ -40,4 +63,8 @@ func publickey() *cobra.Command {
4063
return nil
4164
},
4265
}
66+
cmd.Flags().BoolVar(&reset, "reset", false, "Regenerate your public key. This will require updating the key on any services it's registered with.")
67+
cliui.AllowSkipPrompt(cmd)
68+
69+
return cmd
4370
}

cli/userlist.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,32 @@ func userList() *cobra.Command {
3333
"Specify a column to filter in the table.")
3434
return cmd
3535
}
36+
37+
func userSingle() *cobra.Command {
38+
var (
39+
columns []string
40+
)
41+
cmd := &cobra.Command{
42+
Use: "show <username|user_id|'me'>",
43+
Short: "Show a single user. Use 'me' to indicate the currently authenticated user.",
44+
Example: "coder users show me",
45+
Args: cobra.ExactArgs(1),
46+
RunE: func(cmd *cobra.Command, args []string) error {
47+
client, err := createClient(cmd)
48+
if err != nil {
49+
return err
50+
}
51+
52+
user, err := client.User(cmd.Context(), args[0])
53+
if err != nil {
54+
return err
55+
}
56+
57+
_, err = fmt.Fprintln(cmd.OutOrStdout(), displayUsers(columns, user))
58+
return err
59+
},
60+
}
61+
cmd.Flags().StringArrayVarP(&columns, "column", "c", []string{"username", "email", "created_at"},
62+
"Specify a column to filter in the table.")
63+
return cmd
64+
}

cli/userlist_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package cli_test
22

33
import (
4+
"context"
45
"testing"
56

7+
"github.com/stretchr/testify/assert"
68
"github.com/stretchr/testify/require"
79

810
"github.com/coder/coder/cli/clitest"
@@ -51,3 +53,26 @@ func TestUserList(t *testing.T) {
5153
require.Contains(t, err.Error(), "Try logging in using 'coder login <url>'.")
5254
})
5355
}
56+
57+
func TestUserShow(t *testing.T) {
58+
t.Parallel()
59+
ctx := context.Background()
60+
client := coderdtest.New(t, nil)
61+
admin := coderdtest.CreateFirstUser(t, client)
62+
other := coderdtest.CreateAnotherUser(t, client, admin.OrganizationID)
63+
otherUser, err := other.User(ctx, codersdk.Me)
64+
require.NoError(t, err, "fetch other user")
65+
cmd, root := clitest.New(t, "users", "show", otherUser.Username)
66+
clitest.SetupConfig(t, client, root)
67+
doneChan := make(chan struct{})
68+
pty := ptytest.New(t)
69+
cmd.SetIn(pty.Input())
70+
cmd.SetOut(pty.Output())
71+
go func() {
72+
defer close(doneChan)
73+
err := cmd.Execute()
74+
assert.NoError(t, err)
75+
}()
76+
pty.ExpectMatch(otherUser.Email)
77+
<-doneChan
78+
}

cli/users.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func users() *cobra.Command {
1818
cmd.AddCommand(
1919
userCreate(),
2020
userList(),
21+
userSingle(),
2122
createUserStatusCommand(codersdk.UserStatusActive),
2223
createUserStatusCommand(codersdk.UserStatusSuspended),
2324
)

0 commit comments

Comments
 (0)