Skip to content
New issue

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

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

Already on GitHub? # 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
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@
"luxon": "^3.5.0",
"open": "10.1.0",
"shiki": "^1.15.2",
"table": "^6.9.0",
"update-notifier": "^7.3.1",
"yaml": "^2.6.1",
"yargs": "^17.7.2"
6 changes: 0 additions & 6 deletions src/cli.mjs
Original file line number Diff line number Diff line change
@@ -184,12 +184,6 @@ function buildYargs(argvInput) {
description: "Profile from the CLI config file to use.",
group: "Config:",
},
json: {
type: "boolean",
description: "Output the results as JSON.",
default: false,
group: "Output:",
},
quiet: {
type: "boolean",
description:
40 changes: 28 additions & 12 deletions src/commands/database/create.mjs
Original file line number Diff line number Diff line change
@@ -5,8 +5,25 @@ import { container } from "../../config/container.mjs";
import { CommandError } from "../../lib/errors.mjs";
import { faunaToCommandError } from "../../lib/fauna.mjs";
import { getSecret, retryInvalidCredsOnce } from "../../lib/fauna-client.mjs";
import { colorize, Format } from "../../lib/formatting/colorize.mjs";
import { createFormatter } from "../../lib/formatting/formatter.mjs";
import { validateDatabaseOrSecret } from "../../lib/middleware.mjs";
import { FORMATTABLE_OPTIONS } from "../../lib/options.mjs";

const formatter = createFormatter({
header: "fauna database create",
columns: [
"global_id",
"name",
"coll",
"ts",
"protected",
"typechecked",
"priority",
],
short: {
formatter: ({ global_id, name }) => `${name} (${global_id})`, // eslint-disable-line camelcase
},
});

async function runCreateQuery(secret, argv) {
const { fql } = container.resolve("fauna");
@@ -29,20 +46,18 @@ async function createDatabase(argv) {
const logger = container.resolve("logger");

try {
await retryInvalidCredsOnce(secret, async (secret) =>
const { data } = await retryInvalidCredsOnce(secret, async (secret) =>
runCreateQuery(secret, argv),
);
const dataWithAllFields = {
protected: undefined,
typechecked: undefined,
priority: undefined,
...data,
};

logger.stderr(`Database successfully created.`);

const { color, json } = argv;
if (json) {
logger.stdout(
colorize({ name: argv.name }, { color, format: Format.JSON }),
);
} else {
logger.stdout(argv.name);
}
const { color, format } = argv;
logger.stdout(formatter({ data: dataWithAllFields, color, format }));
} catch (e) {
faunaToCommandError({
err: e,
@@ -76,6 +91,7 @@ async function createDatabase(argv) {

function buildCreateCommand(yargs) {
return yargs
.options(FORMATTABLE_OPTIONS)
.options({
name: {
type: "string",
42 changes: 24 additions & 18 deletions src/commands/database/list.mjs
Original file line number Diff line number Diff line change
@@ -3,21 +3,30 @@ import chalk from "chalk";

import { container } from "../../config/container.mjs";
import { faunaToCommandError } from "../../lib/fauna.mjs";
import { colorize, Format } from "../../lib/formatting/colorize.mjs";
import { createFormatter } from "../../lib/formatting/formatter.mjs";
import { FORMATTABLE_OPTIONS } from "../../lib/options.mjs";

const formatter = createFormatter({
header: "fauna database list",
columns: ["name", "path"],
short: {
formatter: ({ path, name }) => `${path ?? name}`,
},
});

async function listDatabasesWithAccountAPI(argv) {
const { pageSize, database } = argv;
const { maxSize, database } = argv;
const { listDatabases } = container.resolve("accountAPI");
const response = await listDatabases({
pageSize,
pageSize: maxSize,
path: database,
});

return response.results.map(({ path, name }) => ({ path, name }));
}

async function listDatabasesWithSecret(argv) {
const { url, secret, pageSize } = argv;
const { url, secret, maxSize, color } = argv;
const { runQueryFromString } = container.resolve("faunaClientV10");

try {
@@ -26,11 +35,11 @@ async function listDatabasesWithSecret(argv) {
secret,
// This gives us back an array of database names. If we want to
// provide the after token at some point this query will need to be updated.
expression: `Database.all().paginate(${pageSize}).data { name }`,
expression: `Database.all().paginate(${maxSize}).data { name }`,
});
return res.data;
} catch (e) {
return faunaToCommandError({ err: e, color: argv.color });
return faunaToCommandError({ err: e, color });
}
}

@@ -54,22 +63,19 @@ async function doListDatabases(argv) {
);
}

if (argv.json) {
logger.stdout(colorize(res, { format: Format.JSON, color: argv.color }));
} else {
res.forEach(({ path, name }) => {
logger.stdout(path ?? name);
});
}
const { format, color } = argv;
logger.stdout(formatter({ data: res, format, color }));
}

function buildListCommand(yargs) {
return yargs
.options(FORMATTABLE_OPTIONS)
.options({
"page-size": {
"max-size": {
alias: "max",
type: "number",
description: "Maximum number of databases to return.",
default: 1000,
default: 10,
},
})
.example([
@@ -83,12 +89,12 @@ function buildListCommand(yargs) {
"List all child databases directly under a database scoped to a secret.",
],
[
"$0 database list --json",
"$0 database list -f json",
"List all top-level databases and output as JSON.",
],
[
"$0 database list --page-size 10",
"List the first 10 top-level databases.",
"$0 database list --max-size 100",
"List the first 100 top-level databases.",
],
]);
}
14 changes: 7 additions & 7 deletions src/commands/local.mjs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { pushSchema } from "../commands/schema/push.mjs";
import { container } from "../config/container.mjs";
import { ensureContainerRunning } from "../lib/docker-containers.mjs";
import { CommandError, ValidationError } from "../lib/errors.mjs";
import { colorize, Format } from "../lib/formatting/colorize.mjs";
import { colorize, Language } from "../lib/formatting/colorize.mjs";

/**
* Starts the local Fauna container
@@ -40,7 +40,7 @@ async function createDatabaseSchema(argv) {
colorize(
`[CreateDatabaseSchema] Creating schema for database '${argv.database}' from directory '${argv.directory}'...`,
{
format: Format.LOG,
language: Language.LOG,
color: argv.color,
},
),
@@ -52,7 +52,7 @@ async function createDatabaseSchema(argv) {
colorize(
`[CreateDatabaseSchema] Schema for database '${argv.database}' created from directory '${argv.directory}'.`,
{
format: Format.LOG,
language: Language.LOG,
color: argv.color,
},
),
@@ -66,7 +66,7 @@ async function createDatabase(argv) {
const color = argv.color;
logger.stderr(
colorize(`[CreateDatabase] Creating database '${argv.database}'...`, {
format: Format.LOG,
language: Language.LOG,
color,
}),
);
@@ -105,17 +105,17 @@ async function createDatabase(argv) {
});
logger.stderr(
colorize(`[CreateDatabase] Database '${argv.database}' created.`, {
format: Format.LOG,
language: Language.LOG,
color,
}),
);
logger.stderr(colorize(db.data, { format: Format.FQL, color }));
logger.stderr(colorize(db.data, { language: Language.FQL, color }));
} catch (e) {
if (e instanceof AbortError) {
throw new CommandError(
`${chalk.red(`[CreateDatabase] Database '${argv.database}' already exists but with differrent properties than requested:\n`)}
-----------------
${colorize(e.abort, { format: Format.FQL, color })}
${colorize(e.abort, { language: Language.FQL, color })}
-----------------
${chalk.red("Please use choose a different name using --name or align the --typechecked, --priority, and --protected with what is currently present.")}`,
);
Loading
Loading