From 6f5e10110f05b524d85951069eaf9582b9c5957d Mon Sep 17 00:00:00 2001 From: Jyotirmoy Bandyopadhayaya Date: Mon, 8 Jul 2024 13:56:46 +0000 Subject: [PATCH 1/5] feat: added whomai cmd to coder cli --- cli/root.go | 1 + cli/whoami.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 cli/whoami.go 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/whoami.go b/cli/whoami.go new file mode 100644 index 0000000000000..9135ca052e87a --- /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 + deployConfig, _ := client.DeploymentConfig(ctx) + + 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, deployConfig.Values.AccessURL.Scheme+"://"+deployConfig.Values.AccessURL.Host), pretty.Sprint(cliui.DefaultStyles.Keyword, resp.Username)) + return err + }, + } + return cmd +} From 0de8bd4211511b2375cab4b1ed5a5dd61f303192 Mon Sep 17 00:00:00 2001 From: Jyotirmoy Bandyopadhayaya Date: Mon, 8 Jul 2024 15:15:13 +0000 Subject: [PATCH 2/5] refactor: update Coder CLI's whoami command to use client URL instead of deployment config --- cli/whoami.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/whoami.go b/cli/whoami.go index 9135ca052e87a..9da5a674cf101 100644 --- a/cli/whoami.go +++ b/cli/whoami.go @@ -24,13 +24,13 @@ func (r *RootCmd) whoami() *serpent.Command { // Fetch the user info resp, err := client.User(ctx, codersdk.Me) // Get Coder instance url - deployConfig, _ := client.DeploymentConfig(ctx) + 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, deployConfig.Values.AccessURL.Scheme+"://"+deployConfig.Values.AccessURL.Host), pretty.Sprint(cliui.DefaultStyles.Keyword, resp.Username)) + _, _ = 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 }, } From 26691912fc8ad973b553d4f160d4c505be94d825 Mon Sep 17 00:00:00 2001 From: "Jyotirmoy Bandyopadhyaya [Bravo68]" Date: Mon, 8 Jul 2024 18:09:07 +0000 Subject: [PATCH 3/5] feat(cli): add unit tests for the whoami command --- cli/whoami_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 cli/whoami_test.go 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) + }) +} From 31eb7a967e61807cf14beb230522504027c81f9a Mon Sep 17 00:00:00 2001 From: "Jyotirmoy Bandyopadhyaya [Bravo68]" Date: Mon, 8 Jul 2024 18:09:59 +0000 Subject: [PATCH 4/5] chore(docs): add coder command to fetch authenticated user info --- docs/cli.md | 1 + docs/cli/whoami.md | 11 +++++++++++ docs/manifest.json | 5 +++++ 3 files changed, 17 insertions(+) create mode 100644 docs/cli/whoami.md 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" } ] }, From 673df976b8d8b25da37f224c9b81b1e8d160d9fa Mon Sep 17 00:00:00 2001 From: "Jyotirmoy Bandyopadhyaya [Bravo68]" Date: Tue, 9 Jul 2024 16:05:45 +0000 Subject: [PATCH 5/5] chore(doc): update help desc --- cli/testdata/coder_--help.golden | 1 + cli/testdata/coder_whoami_--help.golden | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 cli/testdata/coder_whoami_--help.golden 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.