mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
* 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
39 lines
973 B
Go
39 lines
973 B
Go
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
|
|
}
|