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.
This commit is contained in:
Cian Johnston
2025-09-26 11:20:25 +01:00
committed by GitHub
parent 05537c1894
commit 0a840e48fd
3 changed files with 35 additions and 10 deletions
+1 -1
View File
@@ -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)
+30 -5
View File
@@ -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 {
+4 -4
View File
@@ -13,10 +13,10 @@ coder whoami [flags]
### -c, --column
| | |
|---------|------------------------------|
| Type | <code>[URL\|Username]</code> |
| Default | <code>url,username</code> |
| | |
|---------|-----------------------------------------------|
| Type | <code>[URL\|Username\|ID\|Orgs\|Roles]</code> |
| Default | <code>url,username,id</code> |
Columns to display in table output.