Skip to content

Commit b07e306

Browse files
authored
feat: added whomai cmd to coder cli (#13814)
* feat: added whomai cmd to coder cli * refactor: update Coder CLI's whoami command to use client URL instead of deployment config * feat(cli): add unit tests for the whoami command * chore(docs): add coder command to fetch authenticated user info * chore(doc): update help desc
1 parent 01b30ea commit b07e306

File tree

8 files changed

+103
-0
lines changed

8 files changed

+103
-0
lines changed

cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command {
117117
r.stop(),
118118
r.unfavorite(),
119119
r.update(),
120+
r.whoami(),
120121

121122
// Hidden
122123
r.gitssh(),

cli/testdata/coder_--help.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ SUBCOMMANDS:
5555
date
5656
users Manage users
5757
version Show coder version
58+
whoami Fetch authenticated user info for Coder deployment
5859

5960
GLOBAL OPTIONS:
6061
Global options are applied to all commands. They can be set using environment
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
coder v0.0.0-devel
2+
3+
USAGE:
4+
coder whoami
5+
6+
Fetch authenticated user info for Coder deployment
7+
8+
———
9+
Run `coder --help` for a list of global options.

cli/whoami.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/coder/coder/v2/cli/cliui"
7+
"github.com/coder/coder/v2/codersdk"
8+
"github.com/coder/pretty"
9+
"github.com/coder/serpent"
10+
)
11+
12+
func (r *RootCmd) whoami() *serpent.Command {
13+
client := new(codersdk.Client)
14+
cmd := &serpent.Command{
15+
Annotations: workspaceCommand,
16+
Use: "whoami",
17+
Short: "Fetch authenticated user info for Coder deployment",
18+
Middleware: serpent.Chain(
19+
serpent.RequireNArgs(0),
20+
r.InitClient(client),
21+
),
22+
Handler: func(inv *serpent.Invocation) error {
23+
ctx := inv.Context()
24+
// Fetch the user info
25+
resp, err := client.User(ctx, codersdk.Me)
26+
// Get Coder instance url
27+
clientURL := client.URL
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
_, _ = fmt.Fprintf(inv.Stdout, Caret+"Coder is running at %s, You're authenticated as %s !\n", pretty.Sprint(cliui.DefaultStyles.Keyword, clientURL), pretty.Sprint(cliui.DefaultStyles.Keyword, resp.Username))
34+
return err
35+
},
36+
}
37+
return cmd
38+
}

cli/whoami_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cli_test
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/coder/coder/v2/cli/clitest"
10+
"github.com/coder/coder/v2/coderd/coderdtest"
11+
)
12+
13+
func TestWhoami(t *testing.T) {
14+
t.Parallel()
15+
16+
t.Run("InitialUserNoTTY", func(t *testing.T) {
17+
t.Parallel()
18+
client := coderdtest.New(t, nil)
19+
root, _ := clitest.New(t, "login", client.URL.String())
20+
err := root.Run()
21+
require.Error(t, err)
22+
})
23+
24+
t.Run("OK", func(t *testing.T) {
25+
t.Parallel()
26+
client := coderdtest.New(t, nil)
27+
_ = coderdtest.CreateFirstUser(t, client)
28+
inv, root := clitest.New(t, "whoami")
29+
clitest.SetupConfig(t, client, root)
30+
buf := new(bytes.Buffer)
31+
inv.Stdout = buf
32+
err := inv.Run()
33+
require.NoError(t, err)
34+
whoami := buf.String()
35+
require.NotEmpty(t, whoami)
36+
})
37+
}

docs/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr
5757
| [<code>stop</code>](./cli/stop.md) | Stop a workspace |
5858
| [<code>unfavorite</code>](./cli/unfavorite.md) | Remove a workspace from your favorites |
5959
| [<code>update</code>](./cli/update.md) | Will update and start a given workspace if it is out of date |
60+
| [<code>whoami</code>](./cli/whoami.md) | Fetch authenticated user info for Coder deployment |
6061
| [<code>support</code>](./cli/support.md) | Commands for troubleshooting issues with a Coder deployment. |
6162
| [<code>server</code>](./cli/server.md) | Start a Coder server |
6263
| [<code>features</code>](./cli/features.md) | List Enterprise features |

docs/cli/whoami.md

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/manifest.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,11 @@
10721072
"title": "version",
10731073
"description": "Show coder version",
10741074
"path": "cli/version.md"
1075+
},
1076+
{
1077+
"title": "whoami",
1078+
"description": "Fetch authenticated user info for Coder deployment",
1079+
"path": "cli/whoami.md"
10751080
}
10761081
]
10771082
},

0 commit comments

Comments
 (0)