Support formatting account API commands as tables, tsv, yaml, json, or text
Problem
The current formatting pattern for each of our account API commands is bespoke. We're soon introducing additional support for Account API commands, and we don't want to create even more bespoke outputs.
Solution
Introduce createFormatter that curries an internal function, toFormat, with a command/output-specific configuration.
const formatter = createFormatter({
header: "fauna database list",
columns: ["name", "path"],
short: {
formatter: ({ path, name }) => `${path ?? name}`,
},
});
Then, within the command, outputting becomes as straight-forward as:
logger.stdout(formatter({ data: res, format, color }));
Any command that uses FORMATTABLE_OPTIONS will get access to the format argument to use with the formatter.
-f, --format Output format for the command. [string] [choices: "table", "yaml", "tsv", "short", "json"] [default: "table"]
To not collide with colorize and the existing Format enum, the existing Format has ben renamed to Language to better align with the fact that it's actually the language being highlighted vs an output format.
The expectation is that createFormatter will be used with any Account API or CRUD-like command operating on a resource. Querying and schema and other core APIs will continue to use colorize directly or other bespoke outputs.
Result
This has been applied to the only "account-like" APIs in the CLI atm, db list and db create. Once more account API commands end up in the CLI, we'll expand its usage.
Testing
Command tests have been updated. Need to add tests for the factory, and will do before it's out of draft.