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

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
}