davsclaus opened a new pull request, #27915: URL: https://github.com/apache/camel/pull/27915
Step 4 of [CAMEL-25388](https://issues.apache.org/jira/browse/CAMEL-25388): with `--json`, stdout is exactly one JSON document, also when the command fails, so a script or an AI agent can parse it. ### The contract (`JsonOutput`) - An error prints `{"status":"error","code":3,"message":"..."}` on stdout, with the exit code of the command (CAMEL-25389) in `code`. The `ERROR:` line is still printed on stderr. The central handler does this for every command in JSON mode (`CamelCommand.isJsonOutput()`: the `--json` option, or `--format=json` for `overview`). - A usage error with `--json` (an unknown option, a bad value) prints the same document with code `2`, also when the option comes before `--json`. - A list with nothing in it prints `[]` instead of nothing: `ps`, every `get` and `top` listing, `get error`, `catalog` without a match, `infra get`. - With `--watch`, every refresh prints one more document; the screen is no longer cleared in JSON mode (the escape codes were written to stdout). ### Broken `--json` fixed - `top route` and `top group` printed a table. They now print the JSON of `get route` / `get group`, plus `load01`, `load05` and `load15`. - `get route --error` printed tables: now the routes with an error and their `lastError` (phase, timestamp, message, stack trace). - `get health --trace` printed the stack traces as text after the JSON: they are now `stackTrace` (and `metadata`) in each row. - `get count --total/--fail` printed comma separated counts. - `infra get` printed text and the raw pid file, and `infra stop` ignored `--json`. Both now only see running services (the liveness check of `infra ps`; stale pid files were listed before) and exit `3` when a name matches none. `infra run --background --json` prints `{"pid","alias"}`, and `infra run --json` no longer prints the "Running (use camel infra stop ...)" line after the properties. - `overview --format=json --save/--ai` printed `Saved ...`, the progress and the whole summary before the JSON. These now go to stderr (`--ai` no longer prints the summary in JSON mode). `--apply-descriptions --format=json` prints the changed files as JSON. Errors are exceptions now (a missing directory exits `3`). - `catalog ... --json` with no match printed "No results for filter" as text. It now prints `[]`, and the hint goes to stderr. - `version list`: the `ERROR:` line that broke the MCP `VersionTools` JSON is on stderr since CAMEL-25389 (#27909). ### New `--json` - `doctor`: `{"status","checks":[{"name","status","message"}]}` with `OK`, `WARN` or `FAIL` per check. It exits `1` when a check fails (Maven cannot resolve artifacts, or more than one installation, which already exited `1`). An optional tool (container runtime, Ollama) and ports in use are warnings. This also covers the `doctor` item of CAMEL-25399. The text output is unchanged. - `validate yaml`, `validate properties`, `validate source`: one `{"valid","file","errors"}` per file, the keys of the MCP tool `camel_validate_source`; exit `1` when a file is invalid. - `doc`: the documentation of the MCP tool `camel_catalog_doc` (all options), Kamelets of the asked `--kamelets-version`; not found exits `3` with the same message as the text. - `config get` (`{"key","value"}`), `config list` (`[{"key","value","scope"}]`, in file order), `plugin get` and `plugin list` (`[{"name","command","status":"installed|available",...}]`). The `--json` of the data-returning `cmd` actions moves to CAMEL-25393, where those commands move onto `ActionClient` (#27914). ### Docs - `camel-jbang-exit-codes.adoc`: a "JSON output" section. - 4.24 upgrade guide: "camel-jbang - JSON output". - Generated command docs and metadata for the new options. ### Tests - `JsonOutputContractTest` runs the CLI like `camel` does: error and usage-error documents with their codes, `[]` for `ps`, `get route` (also `--error`), `top route`, `top group`, `get health --trace`, `get count --total`, `infra get`; catalog without match; `infra get kafka` not found; `config get/list`; `doc`; `plugin get/list`; `overview --save` and without routes. - `JsonOutputTest`, `ValidateJsonTest` (yaml, properties, source, skipped file), plus updates to `DoctorTest` (the exit code follows the checks, so it holds on a machine without network), `InfraGetTest`, `ConfigGetTest`, `CatalogDocTest` and `InfraRunPropertyTest`. - `mvn test`: camel-jbang-core (1940 tests before the new ones, and the 94 new/updated ones) and camel-jbang-plugin-validate (21) pass. - Checked as a real process: stdout holds only the document, stderr the `ERROR:` line and hints, and the exit codes are 0/2/3 as expected. _Claude Code on behalf of davsclaus_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01DDRLJAyf8cDXSg8iX785My -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
