Skip to content

Conversation

@ecooper
Copy link

@ecooper ecooper commented Jan 17, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants