diff --git a/cli/root.go b/cli/root.go index 418915490b910..4e615eaa6e7af 100644 --- a/cli/root.go +++ b/cli/root.go @@ -117,6 +117,7 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command { r.stop(), r.unfavorite(), r.update(), + r.whoami(), // Hidden r.gitssh(), diff --git a/cli/testdata/coder_--help.golden b/cli/testdata/coder_--help.golden index ce220e95b1188..a576797d8a48d 100644 --- a/cli/testdata/coder_--help.golden +++ b/cli/testdata/coder_--help.golden @@ -55,6 +55,7 @@ SUBCOMMANDS: date users Manage users version Show coder version + whoami Fetch authenticated user info for Coder deployment GLOBAL OPTIONS: Global options are applied to all commands. They can be set using environment diff --git a/cli/testdata/coder_whoami_--help.golden b/cli/testdata/coder_whoami_--help.golden new file mode 100644 index 0000000000000..9d93ca884f57f --- /dev/null +++ b/cli/testdata/coder_whoami_--help.golden @@ -0,0 +1,9 @@ +coder v0.0.0-devel + +USAGE: + coder whoami + + Fetch authenticated user info for Coder deployment + +——— +Run `coder --help` for a list of global options. diff --git a/cli/whoami.go b/cli/whoami.go new file mode 100644 index 0000000000000..9da5a674cf101 --- /dev/null +++ b/cli/whoami.go @@ -0,0 +1,38 @@ +package cli + +import ( + "fmt" + + "github.com/coder/coder/v2/cli/cliui" + "github.com/coder/coder/v2/codersdk" + "github.com/coder/pretty" + "github.com/coder/serpent" +) + +func (r *RootCmd) whoami() *serpent.Command { + client := new(codersdk.Client) + cmd := &serpent.Command{ + Annotations: workspaceCommand, + Use: "whoami", + Short: "Fetch authenticated user info for Coder deployment", + Middleware: serpent.Chain( + serpent.RequireNArgs(0), + r.InitClient(client), + ), + Handler: func(inv *serpent.Invocation) error { + ctx := inv.Context() + // Fetch the user info + resp, err := client.User(ctx, codersdk.Me) + // Get Coder instance url + clientURL := client.URL + + if err != nil { + return err + } + + _, _ = 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)) + return err + }, + } + return cmd +} diff --git a/cli/whoami_test.go b/cli/whoami_test.go new file mode 100644 index 0000000000000..cdc2f1d8af7a0 --- /dev/null +++ b/cli/whoami_test.go @@ -0,0 +1,37 @@ +package cli_test + +import ( + "bytes" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coder/coder/v2/cli/clitest" + "github.com/coder/coder/v2/coderd/coderdtest" +) + +func TestWhoami(t *testing.T) { + t.Parallel() + + t.Run("InitialUserNoTTY", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, nil) + root, _ := clitest.New(t, "login", client.URL.String()) + err := root.Run() + require.Error(t, err) + }) + + t.Run("OK", func(t *testing.T) { + t.Parallel() + client := coderdtest.New(t, nil) + _ = coderdtest.CreateFirstUser(t, client) + inv, root := clitest.New(t, "whoami") + clitest.SetupConfig(t, client, root) + buf := new(bytes.Buffer) + inv.Stdout = buf + err := inv.Run() + require.NoError(t, err) + whoami := buf.String() + require.NotEmpty(t, whoami) + }) +} diff --git a/docs/cli.md b/docs/cli.md index 7f5364048e3ed..f38bc0e3e133a 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -57,6 +57,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr | [stop](./cli/stop.md) | Stop a workspace | | [unfavorite](./cli/unfavorite.md) | Remove a workspace from your favorites | | [update](./cli/update.md) | Will update and start a given workspace if it is out of date | +| [whoami](./cli/whoami.md) | Fetch authenticated user info for Coder deployment | | [support](./cli/support.md) | Commands for troubleshooting issues with a Coder deployment. | | [server](./cli/server.md) | Start a Coder server | | [features](./cli/features.md) | List Enterprise features | diff --git a/docs/cli/whoami.md b/docs/cli/whoami.md new file mode 100644 index 0000000000000..7e2736d454bf4 --- /dev/null +++ b/docs/cli/whoami.md @@ -0,0 +1,11 @@ + + +# whoami + +Fetch authenticated user info for Coder deployment + +## Usage + +```console +coder whoami +``` diff --git a/docs/manifest.json b/docs/manifest.json index ec09e1b8669c9..dc887921b2b20 100644 --- a/docs/manifest.json +++ b/docs/manifest.json @@ -1072,6 +1072,11 @@ "title": "version", "description": "Show coder version", "path": "cli/version.md" + }, + { + "title": "whoami", + "description": "Fetch authenticated user info for Coder deployment", + "path": "cli/whoami.md" } ] },