diff --git a/src/cli.mjs b/src/cli.mjs index 0f6ccee9..f240cd84 100644 --- a/src/cli.mjs +++ b/src/cli.mjs @@ -104,6 +104,18 @@ function buildYargs(argvInput) { .env("FAUNA") .config("config", configParser) .middleware([checkForUpdates, logArgv], true) + .middleware((argv) => { + if (!argv.url) { + if (argv.local) { + argv.url = 'http://localhost:8443'; + } else { + argv.url = 'https://db.fauna.com'; + } + } + if (!argv.secret && argv.local) { + argv.secret = "secret"; + } + }) .middleware([fixPaths, buildCredentials], false) .command(queryCommand) .command("shell", "start an interactive shell", shellCommand) diff --git a/src/lib/auth/accountKeys.mjs b/src/lib/auth/accountKeys.mjs index 7c8774ce..d933a830 100644 --- a/src/lib/auth/accountKeys.mjs +++ b/src/lib/auth/accountKeys.mjs @@ -24,7 +24,7 @@ export class AccountKeys { // Let users know if the creds they provided are invalid (empty) if (!key && keySource !== "credentials-file") { throw new Error( - `The account key provided by ${keySource} is invalid. Please provide an updated value.`, + `The account key provided by '${keySource}' is invalid. Please provide an updated value.`, ); } } @@ -56,7 +56,7 @@ export class AccountKeys { */ promptLogin() { throw new Error( - `The requested user ${this.user || ""} is not signed in or has expired.\nPlease re-authenticate\n\n + `The requested user '${this.user || ""}' is not signed in or has expired.\nPlease re-authenticate\n\n To sign in, run:\n\nfauna login\n `, ); @@ -69,7 +69,7 @@ export class AccountKeys { async onInvalidCreds() { if (this.keySource !== "credentials-file") { throw new Error( - `Account key provided by ${this.keySource} is invalid. Please provide an updated account key.`, + `Account key provided by '${this.keySource}' is invalid. Please provide an updated account key.`, ); } await this.refreshKey(); diff --git a/src/lib/auth/credentials.mjs b/src/lib/auth/credentials.mjs index 8ec6cdb5..18895f00 100644 --- a/src/lib/auth/credentials.mjs +++ b/src/lib/auth/credentials.mjs @@ -14,7 +14,7 @@ const validateCredentialArgs = (argv) => { // The '--role' option is not supported when using a secret. Secrets have an // implicit role. throw new Error( - "The '--role' option is not supported when using a secret. It will be ignored.", + "The '--role' option is not supported when using a '--secret'. Please specify only one.", ); } }; diff --git a/src/lib/auth/databaseKeys.mjs b/src/lib/auth/databaseKeys.mjs index 3ea59275..efee6515 100644 --- a/src/lib/auth/databaseKeys.mjs +++ b/src/lib/auth/databaseKeys.mjs @@ -46,7 +46,7 @@ export class DatabaseKeys { */ static resolveKeySources(argv, storedKey) { let key, keySource; - // argv.secret come from flag, config, or FAUNA_SECRET + // argv.secret comes from flag, config, or FAUNA_SECRET if (argv.secret) { key = argv.secret; keySource = "user"; diff --git a/src/lib/command-helpers.mjs b/src/lib/command-helpers.mjs index d0260095..eabf2ba1 100644 --- a/src/lib/command-helpers.mjs +++ b/src/lib/command-helpers.mjs @@ -56,8 +56,8 @@ const COMMON_QUERY_OPTIONS = { * @param {string} argv.secret - The secret to use */ export const validateDatabaseOrSecret = (argv) => { - if (!argv.database && !argv.secret) { - throw new Error("No database or secret specified. Pass --database or --secret."); + if (!argv.database && !argv.secret && !argv.local) { + throw new Error("No database or secret specified. Pass either --database, or --secret, or --local."); } } @@ -94,16 +94,5 @@ export function yargsWithCommonConfigurableQueryOptions(yargs) { function yargsWithCommonOptions(yargs, options) { return yargs - .options({ ...options, }) - .check((argv) => { - // If --local is provided and --url is not, set the default URL for local - if (!argv.url) { - if (argv.local) { - argv.url = 'http://localhost:8443'; - } else { - argv.url = 'https://db.fauna.com'; - } - } - return true; // Validation passed - }); + .options({ ...options, }); } diff --git a/src/lib/fauna-client.mjs b/src/lib/fauna-client.mjs index 2dd1d71f..7bd9a35a 100644 --- a/src/lib/fauna-client.mjs +++ b/src/lib/fauna-client.mjs @@ -71,7 +71,11 @@ export default class FaunaClient { } } -export const getSecret = async () => { +/** + * Gets a secret for the current credentials. + * @return {Promise} the secret + */ +export async function getSecret() { const credentials = container.resolve("credentials"); if (!credentials.databaseKeys.key) { return await credentials.databaseKeys.getOrRefreshKey(); diff --git a/test/config.mjs b/test/config.mjs index 1a96a461..e71e32f0 100644 --- a/test/config.mjs +++ b/test/config.mjs @@ -198,7 +198,7 @@ describe("configuration file", function () { }); }); - it("--local arg sets the url to http://localhost:8443 if no URL is given", async function () { + it("--local arg sets the argv.url to http://localhost:8443 if no --url is given", async function () { fs.readdirSync.withArgs(process.cwd()).returns([]); await runBasicTest({ cmd: `eval "Database.all()" --secret "no-config" --local`, @@ -213,7 +213,7 @@ describe("configuration file", function () { }); }); - it("--url arg takes precedence over --local arg for the URL", async function () { + it("--url arg takes precedence over --local arg for the argv.url", async function () { fs.readdirSync.withArgs(process.cwd()).returns([]); await runBasicTest({ cmd: `eval "Database.all()" --secret "no-config" --local --url http://localhost:hibob`, @@ -228,6 +228,36 @@ describe("configuration file", function () { }); }); + it("--local sets the argv.secret to 'secret' if no --secret is given", async function () { + fs.readdirSync.withArgs(process.cwd()).returns([]); + await runBasicTest({ + cmd: `eval "Database.all()" --local`, + argvMatcher: sinon.match({ + apiVersion: "10", + secret: "secret", + url: "http://localhost:8443", + timeout: 5000, + typecheck: undefined, + }), + objectToReturn: databaseObject, + }); + }); + + it("--secret arg takes precedence over --local arg for the argv.secret", async function () { + fs.readdirSync.withArgs(process.cwd()).returns([]); + await runBasicTest({ + cmd: `eval "Database.all()" --local --secret "sauce"`, + argvMatcher: sinon.match({ + apiVersion: "10", + secret: "sauce", + url: "http://localhost:8443", + timeout: 5000, + typecheck: undefined, + }), + objectToReturn: databaseObject, + }); + }); + it("exits with an error if multiple default files exist", async function () { fs.readdirSync .withArgs(process.cwd())