diff --git a/cli/testdata/coder_whoami_--help.golden b/cli/testdata/coder_whoami_--help.golden
index 9d93ca884f..a6313391e8 100644
--- a/cli/testdata/coder_whoami_--help.golden
+++ b/cli/testdata/coder_whoami_--help.golden
@@ -1,9 +1,16 @@
coder v0.0.0-devel
USAGE:
- coder whoami
+ coder whoami [flags]
Fetch authenticated user info for Coder deployment
+OPTIONS:
+ -c, --column [URL|Username] (default: url,username)
+ Columns to display in table output.
+
+ -o, --output text|json|table (default: text)
+ Output format.
+
———
Run `coder --help` for a list of global options.
diff --git a/cli/whoami.go b/cli/whoami.go
index 2765652ebf..504f211422 100644
--- a/cli/whoami.go
+++ b/cli/whoami.go
@@ -9,7 +9,25 @@ import (
"github.com/coder/serpent"
)
+type whoamiRow struct {
+ URL string `json:"url" table:"URL,default_sort"`
+ Username string `json:"username" table:"Username"`
+}
+
+func (r whoamiRow) String() string {
+ return fmt.Sprintf(
+ Caret+"Coder is running at %s, You're authenticated as %s !\n",
+ pretty.Sprint(cliui.DefaultStyles.Keyword, r.URL),
+ pretty.Sprint(cliui.DefaultStyles.Keyword, r.Username),
+ )
+}
+
func (r *RootCmd) whoami() *serpent.Command {
+ formatter := cliui.NewOutputFormatter(
+ cliui.TextFormat(),
+ cliui.JSONFormat(),
+ cliui.TableFormat([]whoamiRow{}, []string{"url", "username"}),
+ )
cmd := &serpent.Command{
Annotations: workspaceCommand,
Use: "whoami",
@@ -28,14 +46,23 @@ func (r *RootCmd) whoami() *serpent.Command {
resp, err := client.User(ctx, codersdk.Me)
// Get Coder instance url
clientURL := client.URL
-
if err != nil {
return err
}
- _, _ = 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))
+ out, err := formatter.Format(ctx, []whoamiRow{
+ {
+ URL: clientURL.String(),
+ Username: resp.Username,
+ },
+ })
+ if err != nil {
+ return err
+ }
+ _, err = inv.Stdout.Write([]byte(out))
return err
},
}
+ formatter.AttachOptions(&cmd.Options)
return cmd
}
diff --git a/docs/reference/cli/whoami.md b/docs/reference/cli/whoami.md
index f3038789f2..2d25f84112 100644
--- a/docs/reference/cli/whoami.md
+++ b/docs/reference/cli/whoami.md
@@ -6,5 +6,25 @@ Fetch authenticated user info for Coder deployment
## Usage
```console
-coder whoami
+coder whoami [flags]
```
+
+## Options
+
+### -c, --column
+
+| | |
+|---------|------------------------------|
+| Type | [URL\|Username] |
+| Default | url,username |
+
+Columns to display in table output.
+
+### -o, --output
+
+| | |
+|---------|--------------------------------|
+| Type | text\|json\|table |
+| Default | text |
+
+Output format.