Skip to content

feat: add JSON output to coder tokens create - #30135

Merged
nickvigilante merged 5 commits into
mainfrom
vigilante/cli-14-coder-tokens-create-add-o-output-with-json
Nov 13, 2026
Merged

nickvigilante merged 5 commits into
mainfrom
vigilante/cli-14-coder-tokens-create-add-o-output-with-json

Conversation

@nickvigilante

@nickvigilante nickvigilante commented Sep 29, 2026 •

Copy link
Copy Markdown
Collaborator

Uses the cliui output helpers from #30508.

coder tokens create printed only the new token's secret, so scripts had to call tokens view afterwards to get the token's ID, expiry, and scopes.

This adds -o/--output with text (default) and json.
The JSON document contains the token's metadata from APIKeyByID plus the one-time key.

Behavior change: Additive.
The default output is still just the secret, so $(coder tokens create ...) keeps working.
The command now makes one extra API call after creating the token; if it fails, the command prints a warning to stderr and still outputs the secret rather than exiting with an error.

Decisions
  • The metadata lookup uses the key ID parsed from the <id>-<secret> token, the same convention tokens view and tokens remove use.
  • A failed metadata lookup never fails the command, because the token already exists and the secret can't be shown again.

Closes CLI-14

This PR was generated by Coder Agents on behalf of @nickvigilante.

@linear-code

linear-code Bot commented Sep 29, 2026

Copy link
Copy Markdown

CLI-14

@nickvigilante
nickvigilante marked this pull request as ready for review September 30, 2026 00:30
@nickvigilante nickvigilante added the cli Area: CLI label Oct 5, 2026
@nickvigilante
nickvigilante force-pushed the vigilante/cli-14-coder-tokens-create-add-o-output-with-json branch from 7ca6d21 to f258d8a Compare October 8, 2026 15:45
@nickvigilante
nickvigilante changed the base branch from main to vigilante/cli-56-cliui-add-shared-text-formatter-and-write-helpers-for-o October 8, 2026 15:45
@nickvigilante
nickvigilante force-pushed the vigilante/cli-14-coder-tokens-create-add-o-output-with-json branch from f258d8a to 809db7d Compare October 8, 2026 18:18
nickvigilante added a commit that referenced this pull request Oct 8, 2026
…pers (#30508)

Adds two small helpers to `cli/cliui/output.go` so commands that add
`-o/--output` don't repeat the same boilerplate, as suggested in
#30152 (comment) by
@jeremyruppel.

- `TextFormatFunc[T]` wraps `ChangeFormatterData(TextFormat(), ...)`
with the type assertion and "expected X, got Y" error, so a command only
supplies its render function.
- `(*OutputFormatter).WriteLine` formats data and writes it to an
`io.Writer` with a trailing newline, replacing the `Format` + `Fprintln`
block.

No existing callers change in this PR. These PRs are stacked on it and
use the helpers:

- #30135 `coder tokens create`
- #30139 `coder ai-gateway keys create`
- #30145 `coder organizations members edit-roles`
- #30152 `coder provisioner keys create`

Migrating the existing callers on `main` (for example
`cli/organization.go`, `enterprise/cli/workspaceproxy.go`) is left for a
follow-up, since some of them print with `Fprint` rather than `Fprintln`
and need care to keep output identical.

Refs CLI-56

<details>
<summary>Decision log</summary>

- A global `-o` flag on the root command was ruled out because `coder
config-ssh` already uses `-o` for `--ssh-option`.
- The "one-time secret plus metadata" response structs in #30135 and
#30152 stay local to each command, since they embed different SDK types
and a shared helper would save little.
- `WriteLine` uses `Fprintln` to match the four stacked PRs and most
existing callers.
It was renamed from `Write` during review so the trailing newline is
visible at call sites and the method is not confused with `io.Writer`.

</details>

> [!NOTE]
> This PR was generated by Coder Agents on behalf of @nickvigilante.
Base automatically changed from vigilante/cli-56-cliui-add-shared-text-formatter-and-write-helpers-for-o to main October 8, 2026 19:41
@jeremyruppel
jeremyruppel requested a review from untra October 16, 2026 18:01
@nickvigilante
nickvigilante force-pushed the vigilante/cli-14-coder-tokens-create-add-o-output-with-json branch from 809db7d to 4f272f5 Compare October 16, 2026 18:35
Comment thread cli/tokens.go Outdated
// The key is formatted as "<id>-<secret>".
keyID, _, ok := strings.Cut(res.Key, "-")
if !ok {
return xerrors.Errorf("malformed token key returned by server")

@untra untra Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could the key be formatted wrong here?
At this point, the token would already be created, so the user never sees the secret. Maybe instead warn and fall through with the key instead.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. The server always formats the key as <10-char id>-<22-char secret> from alphanumerics (coderd/apikey/apikey.go), so this shouldn't happen in practice. You're right that failing here would lose the one-time secret, though.

Fixed in 4c903f1: if the key can't be parsed, the command now warns on stderr, skips the metadata lookup, and still prints the key (with an empty id in JSON output rather than copying the secret into it). Added TestTokenCreateMalformedKey to cover it.

This reply was generated by Coder Agents on behalf of @nickvigilante.

@nickvigilante
nickvigilante force-pushed the vigilante/cli-14-coder-tokens-create-add-o-output-with-json branch from 4f272f5 to 4c903f1 Compare October 21, 2026 17:38
@nickvigilante
nickvigilante requested a review from untra October 21, 2026 17:38
@github-actions github-actions Bot added the stale This issue is like stale bread. label Nov 6, 2026
@github-actions github-actions Bot closed this Nov 9, 2026
@nickvigilante nickvigilante reopened this Nov 10, 2026
@nickvigilante nickvigilante removed the stale This issue is like stale bread. label Nov 10, 2026
@nickvigilante
nickvigilante temporarily deployed to main November 10, 2026 17:35 — with GitHub Actions Inactive
@nickvigilante

Copy link
Copy Markdown
Collaborator Author

@untra Another nudge

@untra

untra commented Nov 10, 2026

Copy link
Copy Markdown
Collaborator

nudge again when light is green

`coder tokens create` printed only the new token's secret, so scripts
had to call `tokens view` afterwards to get the token's ID, expiry, and
scopes.

Linear: CLI-14
The token already exists by this point, so returning an error would
lose the one-time secret. Warn and print the key instead.
@nickvigilante
nickvigilante force-pushed the vigilante/cli-14-coder-tokens-create-add-o-output-with-json branch from 4c903f1 to c749e8c Compare November 12, 2026 14:38
@nickvigilante
nickvigilante temporarily deployed to main November 12, 2026 14:38 — with GitHub Actions Inactive
@nickvigilante
nickvigilante temporarily deployed to main November 12, 2026 15:55 — with GitHub Actions Inactive
@nickvigilante

Copy link
Copy Markdown
Collaborator Author

@untra Nudge

@nickvigilante
nickvigilante merged commit 32e1f25 into main Nov 13, 2026
30 of 31 checks passed
@nickvigilante
nickvigilante deleted the vigilante/cli-14-coder-tokens-create-add-o-output-with-json branch November 13, 2026 17:45
@nickvigilante
nickvigilante temporarily deployed to docs-preview November 13, 2026 17:45 — with GitHub Actions Inactive
@nickvigilante
nickvigilante temporarily deployed to main November 13, 2026 17:45 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 13, 2026

This branch was previously deployed

2 inactive deployments
docs-preview — 22fcc9f1 Deployed Nov 13, 2026 by nickvigilante via cleanup #47
main — 22fcc9f1 Deployed Nov 13, 2026 by nickvigilante via cla #256822
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cli Area: CLI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants