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
This commit is contained in:
Jyotirmoy Bandyopadhayaya
2024-07-09 23:53:11 +05:30
committed by GitHub
parent 01b30eaa32
commit b07e3069dd
8 changed files with 103 additions and 0 deletions

View File

@ -117,6 +117,7 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command {
r.stop(),
r.unfavorite(),
r.update(),
r.whoami(),
// Hidden
r.gitssh(),

View File

@ -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

View File

@ -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
cli/whoami.go Normal file
View File

@ -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
cli/whoami_test.go Normal file
View File

@ -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)
})
}

View File

@ -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 |

11
docs/cli/whoami.md generated Normal file
View File

@ -0,0 +1,11 @@
<!-- DO NOT EDIT | GENERATED CONTENT -->
# whoami
Fetch authenticated user info for Coder deployment
## Usage
```console
coder whoami
```

View File

@ -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"
}
]
},