Skip to content

Commit

Permalink
Merge branch 'main' into export
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper authored Jan 17, 2025
2 parents 19a4f3f + 36461e2 commit b265bdb
Show file tree
Hide file tree
Showing 29 changed files with 524 additions and 301 deletions.
3 changes: 2 additions & 1 deletion src/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
checkForUpdates,
fixPaths,
logArgv,
scopeSecret,
} from "./lib/middleware.mjs";

/** @type {import('yargs').Argv} */
Expand Down Expand Up @@ -112,7 +113,7 @@ function buildYargs(argvInput) {
.config("config", configParser.bind(null, argvInput))
.middleware([checkForUpdates, logArgv], true)
.middleware(
[applyLocalArg, fixPaths, applyAccountUrl, buildCredentials],
[applyLocalArg, fixPaths, applyAccountUrl, buildCredentials, scopeSecret],
false,
)
.command(loginCommand)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function runCreateQuery(secret, argv) {
}

async function createDatabase(argv) {
const secret = await getSecret();
const secret = await getSecret(argv);
const logger = container.resolve("logger");

try {
Expand All @@ -47,6 +47,7 @@ async function createDatabase(argv) {
faunaToCommandError({
err: e,
color: argv.color,
include: argv.include,
handler: (err) => {
if (err instanceof ServiceError && err.code === "constraint_failure") {
const cf = err.constraint_failures;
Expand Down
3 changes: 2 additions & 1 deletion src/commands/database/delete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function runDeleteQuery(secret, argv) {
}

async function deleteDatabase(argv) {
const secret = await getSecret();
const secret = await getSecret(argv);
const logger = container.resolve("logger");

try {
Expand All @@ -33,6 +33,7 @@ async function deleteDatabase(argv) {
faunaToCommandError({
err,
color: argv.color,
include: argv.include,
handler: (err) => {
if (err instanceof ServiceError && err.code === "document_not_found") {
throw new CommandError(
Expand Down
6 changes: 5 additions & 1 deletion src/commands/database/list.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ async function listDatabasesWithSecret(argv) {
});
return res.data;
} catch (e) {
return faunaToCommandError({ err: e, color: argv.color });
return faunaToCommandError({
err: e,
color: argv.color,
include: argv.include,
});
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/commands/local.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ async function createDatabaseSchema(argv) {
},
),
);
// hack to let us push schema to the local database
argv.secret = `secret:${argv.database}:admin`;
await pushSchema({ ...argv, active: true, input: false });
logger.stderr(
colorize(
Expand Down
28 changes: 13 additions & 15 deletions src/commands/query.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,22 @@ async function queryCommand(argv) {
validateDatabaseOrSecret(argv);
validate(argv);

const secret = await getSecret(argv);
const {
url,
timeout,
typecheck,
apiVersion,
performanceHints,
color,
include,
} = argv;

// resolve the input
const expression = resolveInput(argv);

// get the query handler and run the query
try {
const secret = await getSecret();
const {
url,
timeout,
typecheck,
apiVersion,
performanceHints,
color,
include,
} = argv;

// If we're writing to a file, don't colorize the output regardless of the user's preference
const useColor = argv.output || !isTTY() ? false : color;

Expand All @@ -115,9 +115,7 @@ async function queryCommand(argv) {
color: useColor,
});

// If any query info should be displayed, print to stderr.
// This is only supported in v10.
if (include.length > 0 && apiVersion === "10") {
if (include.length > 0) {
const queryInfo = formatQueryInfo(results, {
apiVersion,
color: useColor,
Expand Down Expand Up @@ -147,7 +145,7 @@ async function queryCommand(argv) {
}

const { apiVersion, color } = argv;
throw new CommandError(formatError(err, { apiVersion, color }), {
throw new CommandError(formatError(err, { apiVersion, color, include }), {
cause: err,
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function doAbandon(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");
const logger = container.resolve("logger");
const confirm = container.resolve("confirm");
const secret = await getSecret();
const secret = await getSecret(argv);

if (!argv.input) {
const params = new URLSearchParams({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/commit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function doCommit(argv) {
const makeFaunaRequest = container.resolve("makeFaunaRequest");
const logger = container.resolve("logger");
const confirm = container.resolve("confirm");
const secret = await getSecret();
const secret = await getSecret(argv);

if (!argv.input) {
const params = new URLSearchParams({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async function doDiff(argv) {
const gatherFSL = container.resolve("gatherFSL");
const logger = container.resolve("logger");
const makeFaunaRequest = container.resolve("makeFaunaRequest");
const secret = await getSecret();
const secret = await getSecret(argv);
const files = reformatFSL(await gatherFSL(argv.dir));

const { version, status, diff } = await makeFaunaRequest({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/pull.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async function doPull(argv) {
const logger = container.resolve("logger");
const confirm = container.resolve("confirm");
const makeFaunaRequest = container.resolve("makeFaunaRequest");
const secret = await getSecret();
const secret = await getSecret(argv);

// Get the staged schema status
/** @type {{ status: "none" | "pending" | "ready" | "failed", version: string }} */
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function pushSchema(argv) {
const gatherFSL = container.resolve("gatherFSL");

const isStagedPush = !argv.active;
const secret = await getSecret();
const secret = await getSecret(argv);
const fslFiles = await gatherFSL(argv.dir);
const hasLocalSchema = fslFiles.length > 0;
const absoluteDirPath = path.resolve(argv.dir);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function doStatus(argv) {
const logger = container.resolve("logger");
const makeFaunaRequest = container.resolve("makeFaunaRequest");

const secret = await getSecret();
const secret = await getSecret(argv);
const absoluteDirPath = path.resolve(argv.dir);
const gatherFSL = container.resolve("gatherFSL");
const fslFiles = await gatherFSL(argv.dir);
Expand Down
15 changes: 9 additions & 6 deletions src/commands/shell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import * as esprima from "esprima";
import { container } from "../config/container.mjs";
import { formatQueryResponse, getSecret } from "../lib/fauna-client.mjs";
import { clearHistoryStorage, initHistoryStorage } from "../lib/file-util.mjs";
import { validateDatabaseOrSecret } from "../lib/middleware.mjs";
import {
resolveIncludeOptions,
validateDatabaseOrSecret,
} from "../lib/middleware.mjs";
import {
ACCOUNT_OPTIONS,
CORE_OPTIONS,
Expand All @@ -25,7 +28,7 @@ async function shellCommand(argv) {

// Fast fail if the database is not queryable
const isQueryable = container.resolve("isQueryable");
await isQueryable({ ...argv, secret: await getSecret() });
await isQueryable({ ...argv, secret: await getSecret(argv) });

const logger = container.resolve("logger");
let completionPromise;
Expand Down Expand Up @@ -176,7 +179,7 @@ async function buildCustomEval(argv) {

let res;
try {
const secret = await getSecret();
const secret = await getSecret(argv);
const { color, timeout, typecheck, url } = argv;

res = await runQueryFromString(cmd, {
Expand All @@ -190,8 +193,7 @@ async function buildCustomEval(argv) {
});

// If any query info should be displayed, print to stderr.
// This is only supported in v10.
if (include.length > 0 && apiVersion === "10") {
if (include.length > 0) {
const queryInfo = formatQueryInfo(res, {
apiVersion,
color,
Expand All @@ -202,7 +204,7 @@ async function buildCustomEval(argv) {
}
}
} catch (err) {
logger.stderr(formatError(err, { apiVersion, color }));
logger.stderr(formatError(err, { apiVersion, color, include }));
return cb(null);
}

Expand All @@ -227,6 +229,7 @@ function buildShellCommand(yargs) {
.options(DATABASE_PATH_OPTIONS)
.options(CORE_OPTIONS)
.options(QUERY_OPTIONS)
.middleware(resolveIncludeOptions)
.example([
[
"$0 shell --database us/my_db",
Expand Down
2 changes: 2 additions & 0 deletions src/config/setup-test-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ export function setupTestContainer() {
runQuery: stub(),
runQueryFromString: stub(),
formatQueryResponse: faunaClientV10.formatQueryResponse,
formatQueryInfo: faunaClientV10.formatQueryInfo,
formatError: faunaClientV10.formatError,
}),
faunaClientV4: awilix.asValue({
getClient: stub(),
runQuery: stub(),
runQueryFromString: stub(),
formatQueryResponse: faunaClientV4.formatQueryResponse,
formatQueryInfo: faunaClientV4.formatQueryInfo,
formatError: faunaClientV4.formatError,
}),
sleep: awilix.asValue(stub().resolves()),
Expand Down
17 changes: 12 additions & 5 deletions src/lib/auth/credentials.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import { DatabaseKeys } from "./databaseKeys.mjs";

const validateCredentialArgs = (argv) => {
const logger = container.resolve("logger");
const illegalArgCombos = [
["accountKey", "secret", isLocal],
["secret", "database", isLocal],
["secret", "role", isLocal],
];
const illegalArgCombos = [["accountKey", "secret", isLocal]];
for (const [first, second, conditional] of illegalArgCombos) {
if (argv[first] && argv[second] && !conditional(argv)) {
throw new ValidationError(
Expand Down Expand Up @@ -67,6 +63,17 @@ export class Credentials {
});
this.accountKeys.key = accountKey;
}

/**
* Gets a secret for the current credentials.
* @return {Promise<string>} the secret
*/
async getSecret() {
if (!this.databaseKeys.key) {
return await this.databaseKeys.getOrRefreshKey();
}
return this.databaseKeys.key;
}
}

/**
Expand Down
Loading

0 comments on commit b265bdb

Please sign in to comment.