Skip to content

Commit 6f5e101

Browse files
committed
feat: added whomai cmd to coder cli
1 parent 10aa32c commit 6f5e101

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

cli/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func (r *RootCmd) CoreSubcommands() []*serpent.Command {
117117
r.stop(),
118118
r.unfavorite(),
119119
r.update(),
120+
r.whoami(),
120121

121122
// Hidden
122123
r.gitssh(),

cli/whoami.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cli
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/coder/coder/v2/cli/cliui"
7+
"github.com/coder/coder/v2/codersdk"
8+
"github.com/coder/pretty"
9+
"github.com/coder/serpent"
10+
)
11+
12+
func (r *RootCmd) whoami() *serpent.Command {
13+
client := new(codersdk.Client)
14+
cmd := &serpent.Command{
15+
Annotations: workspaceCommand,
16+
Use: "whoami",
17+
Short: "Fetch authenticated user info for Coder deployment",
18+
Middleware: serpent.Chain(
19+
serpent.RequireNArgs(0),
20+
r.InitClient(client),
21+
),
22+
Handler: func(inv *serpent.Invocation) error {
23+
ctx := inv.Context()
24+
// Fetch the user info
25+
resp, err := client.User(ctx, codersdk.Me)
26+
// Get Coder instance url
27+
deployConfig, _ := client.DeploymentConfig(ctx)
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
_, _ = fmt.Fprintf(inv.Stdout, Caret+"Coder is running at %s, You're authenticated as %s !\n", pretty.Sprint(cliui.DefaultStyles.Keyword, deployConfig.Values.AccessURL.Scheme+"://"+deployConfig.Values.AccessURL.Host), pretty.Sprint(cliui.DefaultStyles.Keyword, resp.Username))
34+
return err
35+
},
36+
}
37+
return cmd
38+
}

0 commit comments

Comments
 (0)