Skip to content

feat: added whomai cmd to coder cli #13814

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command {
r.stop(),
r.unfavorite(),
r.update(),
r.whoami(),

// Hidden
r.gitssh(),
Expand Down
1 change: 1 addition & 0 deletions cli/testdata/coder_--help.golden
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions cli/testdata/coder_whoami_--help.golden
Original file line number Diff line number Diff line change
@@ -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.
38 changes: 38 additions & 0 deletions cli/whoami.go
Original file line number Diff line number Diff line change
@@ -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
}
37 changes: 37 additions & 0 deletions cli/whoami_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
1 change: 1 addition & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Coder — A tool for provisioning self-hosted development environments with Terr
| [<code>stop</code>](./cli/stop.md) | Stop a workspace |
| [<code>unfavorite</code>](./cli/unfavorite.md) | Remove a workspace from your favorites |
| [<code>update</code>](./cli/update.md) | Will update and start a given workspace if it is out of date |
| [<code>whoami</code>](./cli/whoami.md) | Fetch authenticated user info for Coder deployment |
| [<code>support</code>](./cli/support.md) | Commands for troubleshooting issues with a Coder deployment. |
| [<code>server</code>](./cli/server.md) | Start a Coder server |
| [<code>features</code>](./cli/features.md) | List Enterprise features |
Expand Down
11 changes: 11 additions & 0 deletions docs/cli/whoami.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
},
Expand Down
Loading