Repository navigation
feat: add JSON output to coder tokens create - #30135
nickvigilante merged 5 commits into
Conversation
7ca6d21 to
f258d8a
Compare
f258d8a to
809db7d
Compare
…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.
809db7d to
4f272f5
Compare
| // The key is formatted as "<id>-<secret>". | ||
| keyID, _, ok := strings.Cut(res.Key, "-") | ||
| if !ok { | ||
| return xerrors.Errorf("malformed token key returned by server") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
4f272f5 to
4c903f1
Compare
|
@untra Another nudge |
|
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.
4c903f1 to
c749e8c
Compare
|
@untra Nudge |
Uses the
cliuioutput helpers from #30508.coder tokens createprinted only the new token's secret, so scripts had to calltokens viewafterwards to get the token's ID, expiry, and scopes.This adds
-o/--outputwithtext(default) andjson.The JSON document contains the token's metadata from
APIKeyByIDplus the one-timekey.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
<id>-<secret>token, the same conventiontokens viewandtokens removeuse.Closes CLI-14
This PR was generated by Coder Agents on behalf of @nickvigilante.