Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit930bbaf

Browse files
authored
feat(cli): add formatting options to coder whoami (#19970)
This addresses a long-standing gripe of mine: to get your logged inusername you would have to do```bashcoder whoami | awk '{print $9}'```This allows you to do:```coder whoami -o json | jq -r '.username'```or```coder whoami -f table -c username```
1 parentf290472 commit930bbaf

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
coder v0.0.0-devel
22

33
USAGE:
4-
coder whoami
4+
coder whoami [flags]
55

66
Fetch authenticated user info for Coder deployment
77

8+
OPTIONS:
9+
-c, --column [URL|Username] (default: url,username)
10+
Columns to display in table output.
11+
12+
-o, --output text|json|table (default: text)
13+
Output format.
14+
815
———
916
Run `coder --help` for a list of global options.

‎cli/whoami.go‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,25 @@ import (
99
"github.com/coder/serpent"
1010
)
1111

12+
typewhoamiRowstruct {
13+
URLstring`json:"url" table:"URL,default_sort"`
14+
Usernamestring`json:"username" table:"Username"`
15+
}
16+
17+
func (rwhoamiRow)String()string {
18+
returnfmt.Sprintf(
19+
Caret+"Coder is running at %s, You're authenticated as %s !\n",
20+
pretty.Sprint(cliui.DefaultStyles.Keyword,r.URL),
21+
pretty.Sprint(cliui.DefaultStyles.Keyword,r.Username),
22+
)
23+
}
24+
1225
func (r*RootCmd)whoami()*serpent.Command {
26+
formatter:=cliui.NewOutputFormatter(
27+
cliui.TextFormat(),
28+
cliui.JSONFormat(),
29+
cliui.TableFormat([]whoamiRow{}, []string{"url","username"}),
30+
)
1331
cmd:=&serpent.Command{
1432
Annotations:workspaceCommand,
1533
Use:"whoami",
@@ -28,14 +46,23 @@ func (r *RootCmd) whoami() *serpent.Command {
2846
resp,err:=client.User(ctx,codersdk.Me)
2947
// Get Coder instance url
3048
clientURL:=client.URL
31-
3249
iferr!=nil {
3350
returnerr
3451
}
3552

36-
_,_=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))
53+
out,err:=formatter.Format(ctx, []whoamiRow{
54+
{
55+
URL:clientURL.String(),
56+
Username:resp.Username,
57+
},
58+
})
59+
iferr!=nil {
60+
returnerr
61+
}
62+
_,err=inv.Stdout.Write([]byte(out))
3763
returnerr
3864
},
3965
}
66+
formatter.AttachOptions(&cmd.Options)
4067
returncmd
4168
}

‎docs/reference/cli/whoami.md‎

Lines changed: 21 additions & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp