Skip to content

Commit

Permalink
add first draft of interactive init command
Browse files Browse the repository at this point in the history
  • Loading branch information
echo-bravo-yahoo committed Dec 9, 2024
1 parent fabed04 commit cac55f3
Show file tree
Hide file tree
Showing 8 changed files with 629 additions and 51 deletions.
48 changes: 19 additions & 29 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"eslint": "^9.12.0",
"fauna": "^2.3.0",
"faunadb": "^4.5.4",
"inquirer": "^12.0.0",
"inquirer-file-selector": "^0.6.1",
"json-colorizer": "^3.0.1",
"luxon": "^3.5.0",
"open": "10.1.0",
Expand Down
4 changes: 3 additions & 1 deletion src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import chalk from "chalk";
import yargs from "yargs";

import databaseCommand from "./commands/database/database.mjs";
import initCommand from "./commands/init.mjs";
import keyCommand from "./commands/key/key.mjs";
import loginCommand from "./commands/login.mjs";
import queryCommand from "./commands/query.mjs";
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function run(_argvInput, _container) {
epilogue = `\n${BUG_REPORT_MESSAGE}`;

logger.debug(`unknown error thrown: ${e.name}`, "error");
logger.debug(util.inspect(e, true, 2, false), "error");
logger.debug(util.inspect(e, true, 100, false), "error");
} else {
// Otherwise, just use the error message
subMessage = chalk.red(e.message);
Expand Down Expand Up @@ -147,6 +148,7 @@ function buildYargs(argvInput) {
.command(keyCommand)
.command(schemaCommand)
.command(databaseCommand)
.command(initCommand)
.demandCommand()
.strictCommands(true)
.options({
Expand Down
41 changes: 22 additions & 19 deletions src/commands/database/list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,29 @@ function pickOutputFields(databases, argv) {
);
}

async function listDatabasesWithAccountAPI(argv) {
const { pageSize, database, json, color } = argv;
export async function listDatabasesWithAccountAPI(argv) {
const { pageSize, database } = argv;
const accountClient = new FaunaAccountClient();
const response = await accountClient.listDatabases({
pageSize,
path: database,
});
const output = pickOutputFields(response.results, argv);

if (json) {
container.resolve("logger").stdout(JSON.stringify(output));
} else {
container.resolve("logger").stdout(formatObjectForShell(output, { color }));
}
return pickOutputFields(response.results, argv);
}

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

try {
const result = await runQueryFromString({
return await runQueryFromString({
url,
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 { ${getOutputFields(argv)} }`,
});
container
.resolve("logger")
.stdout(formatQueryResponse(result, { json, color }));
} catch (e) {
if (e instanceof FaunaError) {
throwForError(e);
Expand All @@ -69,11 +60,23 @@ async function listDatabasesWithSecret(argv) {
}

async function listDatabases(argv) {
let databases;
if (argv.secret) {
return listDatabasesWithSecret(argv);
databases = listDatabasesWithSecret(argv);
} else {
return listDatabasesWithAccountAPI(argv);
databases = listDatabasesWithAccountAPI(argv);
}

if (argv.json) {
return JSON.stringify(databases);
} else {
return formatObjectForShell(databases, { color: argv.color });
}
}

async function doListDatabases(argv) {
const logger = container.resolve("logger");
logger.stdout(await listDatabases(argv));
}

function buildListCommand(yargs) {
Expand Down Expand Up @@ -110,5 +113,5 @@ export default {
command: "list",
description: "List databases.",
builder: buildListCommand,
handler: listDatabases,
handler: doListDatabases,
};
Loading

0 comments on commit cac55f3

Please sign in to comment.