Merge pull request #1230 from bbassie/feature/ctl-output-json

Add --output json to clusterioctl
This commit is contained in:
Cooldude2606 authored and GitHub committed 2026-11-20 15:22:38 +00:00
commit 34deacb881
26 files changed
+1048 -347

No files matched your search

+14
View File
@@ -995,6 +995,20 @@ For a command the `definition` is the arguments to pass to [yargs.command](http:
The `handler` is an async function that's invoked when the command is executed and it's passed the parsed command line arguments and a reference to the `Ctl` class of clusterioctl.
Note that messages sent from clusterioctl needs to have `"control"` as a part of `src` for it to be accepted by the controller, see [Defining Link Messages](#defining-link-messages) for how to define the messages that can be sent to the controller.
Commands print their results through `ctl.output` so that `--output json` works for them:
```js
handler: async function(args, ctl) {
const result = await ctl.sendTo("controller", new messages.FrobnicateRequest(args.type));
ctl.output.result(result, () => console.log(`Frobnicated ${result.count} foos`));
},
```
`ctl.output.result(data, human)` prints `data` as JSON in json and wire mode and calls `human` otherwise.
`ctl.output.table(rows, map)` prints an array of objects as a JSON array, or as a table in human mode with `map` applied to each row first.
`ctl.output.human` is true when the output is meant for reading.
Commands that print nothing get `Command ran successfully` or `{"success": true}` printed for them, and errors thrown from the handler are printed as `{"error": "<ClassName>", "message": "<message>"}` in json and wire mode.
To have the command tree become part of clusterioctl it needs to be added to the rootCommand tree in the `addCommands` hook of the ctl entrypoint:
```js