fauna-shell icon indicating copy to clipboard operation
fauna-shell copied to clipboard

Support formatting account API commands as tables, tsv, yaml, json, or text

Open ecooper opened this issue 11 months ago • 0 comments

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.

Screenshot 2025-01-16 at 4 11 45 PM

Testing

Command tests have been updated. Need to add tests for the factory, and will do before it's out of draft.

ecooper avatar Jan 17 '25 00:01 ecooper