From 0a840e48fda22e603ae11b9874b2c6bb5a769333 Mon Sep 17 00:00:00 2001 From: Cian Johnston Date: Fri, 26 Sep 2025 11:20:25 +0100 Subject: [PATCH] feat(cli): add more information to `coder whoami` (#19971) Builds upon https://github.com/coder/coder/pull/19970 I got kinda carried away when I saw the extra stuff we could add in here, so I went ahead and added it: * User ID * Organization IDs * Roles This technically duplicates functionality from `coder users show` but I figure folks may find it useful. --- cli/testdata/coder_whoami_--help.golden | 2 +- cli/whoami.go | 35 +++++++++++++++++++++---- docs/reference/cli/whoami.md | 8 +++--- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/cli/testdata/coder_whoami_--help.golden b/cli/testdata/coder_whoami_--help.golden index a6313391e8..3edd93181c 100644 --- a/cli/testdata/coder_whoami_--help.golden +++ b/cli/testdata/coder_whoami_--help.golden @@ -6,7 +6,7 @@ USAGE: Fetch authenticated user info for Coder deployment OPTIONS: - -c, --column [URL|Username] (default: url,username) + -c, --column [URL|Username|ID|Orgs|Roles] (default: url,username,id) Columns to display in table output. -o, --output text|json|table (default: text) diff --git a/cli/whoami.go b/cli/whoami.go index 504f211422..b5267ae203 100644 --- a/cli/whoami.go +++ b/cli/whoami.go @@ -2,6 +2,7 @@ package cli import ( "fmt" + "strings" "github.com/coder/coder/v2/cli/cliui" "github.com/coder/coder/v2/codersdk" @@ -10,8 +11,13 @@ import ( ) type whoamiRow struct { - URL string `json:"url" table:"URL,default_sort"` - Username string `json:"username" table:"Username"` + URL string `json:"url" table:"URL,default_sort"` + Username string `json:"username" table:"Username"` + UserID string `json:"user_id" table:"ID"` + OrganizationIDs string `json:"-" table:"Orgs"` + OrganizationIDsJSON []string `json:"organization_ids" table:"-"` + Roles string `json:"-" table:"Roles"` + RolesJSON map[string][]string `json:"roles" table:"-"` } func (r whoamiRow) String() string { @@ -26,7 +32,7 @@ func (r *RootCmd) whoami() *serpent.Command { formatter := cliui.NewOutputFormatter( cliui.TextFormat(), cliui.JSONFormat(), - cliui.TableFormat([]whoamiRow{}, []string{"url", "username"}), + cliui.TableFormat([]whoamiRow{}, []string{"url", "username", "id"}), ) cmd := &serpent.Command{ Annotations: workspaceCommand, @@ -50,10 +56,29 @@ func (r *RootCmd) whoami() *serpent.Command { return err } + orgIDs := make([]string, 0, len(resp.OrganizationIDs)) + for _, orgID := range resp.OrganizationIDs { + orgIDs = append(orgIDs, orgID.String()) + } + + roles := make([]string, 0, len(resp.Roles)) + jsonRoles := make(map[string][]string) + for _, role := range resp.Roles { + if role.OrganizationID == "" { + role.OrganizationID = "*" + } + roles = append(roles, fmt.Sprintf("%s:%s", role.OrganizationID, role.DisplayName)) + jsonRoles[role.OrganizationID] = append(jsonRoles[role.OrganizationID], role.DisplayName) + } out, err := formatter.Format(ctx, []whoamiRow{ { - URL: clientURL.String(), - Username: resp.Username, + URL: clientURL.String(), + Username: resp.Username, + UserID: resp.ID.String(), + OrganizationIDs: strings.Join(orgIDs, ","), + OrganizationIDsJSON: orgIDs, + Roles: strings.Join(roles, ","), + RolesJSON: jsonRoles, }, }) if err != nil { diff --git a/docs/reference/cli/whoami.md b/docs/reference/cli/whoami.md index 2d25f84112..9fb9f303c9 100644 --- a/docs/reference/cli/whoami.md +++ b/docs/reference/cli/whoami.md @@ -13,10 +13,10 @@ coder whoami [flags] ### -c, --column -| | | -|---------|------------------------------| -| Type | [URL\|Username] | -| Default | url,username | +| | | +|---------|-----------------------------------------------| +| Type | [URL\|Username\|ID\|Orgs\|Roles] | +| Default | url,username,id | Columns to display in table output.