Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ecooper
Copy link
Contributor

@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.

1 participant