mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
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:
committed by
GitHub
parent
01b30eaa32
commit
b07e3069dd
38
cli/whoami.go
Normal file
38
cli/whoami.go
Normal 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
|
||||
}
|
Reference in New Issue
Block a user