MCPcopy Index your code
hub / github.com/coder/coder / whoami

Method whoami

cli/whoami.go:31–93  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

29}
30
31func (r *RootCmd) whoami() *serpent.Command {
32 formatter := cliui.NewOutputFormatter(
33 cliui.TextFormat(),
34 cliui.JSONFormat(),
35 cliui.TableFormat([]whoamiRow{}, []string{"url", "username", "id"}),
36 )
37 cmd := &serpent.Command{
38 Annotations: workspaceCommand,
39 Use: "whoami",
40 Short: "Fetch authenticated user info for Coder deployment",
41 Middleware: serpent.Chain(
42 serpent.RequireNArgs(0),
43 ),
44 Handler: func(inv *serpent.Invocation) error {
45 client, err := r.InitClient(inv)
46 if err != nil {
47 return err
48 }
49
50 ctx := inv.Context()
51 // Fetch the user info
52 resp, err := client.User(ctx, codersdk.Me)
53 // Get Coder instance url
54 clientURL := client.URL
55 if err != nil {
56 return err
57 }
58
59 orgIDs := make([]string, 0, len(resp.OrganizationIDs))
60 for _, orgID := range resp.OrganizationIDs {
61 orgIDs = append(orgIDs, orgID.String())
62 }
63
64 roles := make([]string, 0, len(resp.Roles))
65 jsonRoles := make(map[string][]string)
66 for _, role := range resp.Roles {
67 if role.OrganizationID == "" {
68 role.OrganizationID = "*"
69 }
70 roles = append(roles, fmt.Sprintf("%s:%s", role.OrganizationID, role.DisplayName))
71 jsonRoles[role.OrganizationID] = append(jsonRoles[role.OrganizationID], role.DisplayName)
72 }
73 out, err := formatter.Format(ctx, []whoamiRow{
74 {
75 URL: clientURL.String(),
76 Username: resp.Username,
77 UserID: resp.ID.String(),
78 OrganizationIDs: strings.Join(orgIDs, ","),
79 OrganizationIDsJSON: orgIDs,
80 Roles: strings.Join(roles, ","),
81 RolesJSON: jsonRoles,
82 },
83 })
84 if err != nil {
85 return err
86 }
87 _, err = inv.Stdout.Write([]byte(out))
88 return err

Callers 1

CoreSubcommandsMethod · 0.95

Calls 11

InitClientMethod · 0.95
FormatMethod · 0.95
AttachOptionsMethod · 0.95
NewOutputFormatterFunction · 0.92
TextFormatFunction · 0.92
JSONFormatFunction · 0.92
TableFormatFunction · 0.92
ContextMethod · 0.65
UserMethod · 0.65
WriteMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected