diff --git a/src/commands/database/create.mjs b/src/commands/database/create.mjs index e00bc3a6..0a9c301d 100644 --- a/src/commands/database/create.mjs +++ b/src/commands/database/create.mjs @@ -6,12 +6,7 @@ import { container } from "../../cli.mjs"; import { throwForError } from "../../lib/fauna.mjs"; import { getSecret, retryInvalidCredsOnce } from "../../lib/fauna-client.mjs"; import { formatObjectForShell } from "../../lib/misc.mjs"; -import { validateSecretOrDatabase } from "./database.mjs"; - -function validate(argv) { - validateSecretOrDatabase(argv); - return true; -} +import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs"; async function runCreateQuery(secret, argv) { const { fql } = container.resolve("fauna"); @@ -80,7 +75,7 @@ function buildCreateCommand(yargs) { description: "User-defined priority for the database.", }, }) - .check(validate) + .check(validateDatabaseOrSecret) .help("help", "show help") .example([ [ diff --git a/src/commands/database/database.mjs b/src/commands/database/database.mjs index 657b83a1..80e6a60d 100644 --- a/src/commands/database/database.mjs +++ b/src/commands/database/database.mjs @@ -14,17 +14,6 @@ function buildDatabase(yargs) { .help("help", "Show help."); } -export function validateSecretOrDatabase(argv) { - // Makes sure the user has provided either a secret or a database so we can - // successfully authenticate them. - if (!argv.secret && !argv.database) { - throw new Error( - "No secret or database provided. Please use either --secret or --database.", - ); - } - return true; -} - export default { command: "database ", aliases: ["db"], diff --git a/src/commands/database/delete.mjs b/src/commands/database/delete.mjs index 909b1267..f458445f 100644 --- a/src/commands/database/delete.mjs +++ b/src/commands/database/delete.mjs @@ -5,12 +5,7 @@ import { FaunaError } from "fauna"; import { container } from "../../cli.mjs"; import { throwForError } from "../../lib/fauna.mjs"; import { getSecret, retryInvalidCredsOnce } from "../../lib/fauna-client.mjs"; -import { validateSecretOrDatabase } from "./database.mjs"; - -function validate(argv) { - validateSecretOrDatabase(argv); - return true; -} +import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs"; async function runDeleteQuery(secret, argv) { const { fql } = container.resolve("fauna"); @@ -53,7 +48,7 @@ function buildDeleteCommand(yargs) { description: "Name of the database to delete.", }, }) - .check(validate) + .check(validateDatabaseOrSecret) .help("help", "Show help.") .example([ [ diff --git a/src/lib/command-helpers.mjs b/src/lib/command-helpers.mjs index 4c230583..0d948981 100644 --- a/src/lib/command-helpers.mjs +++ b/src/lib/command-helpers.mjs @@ -143,6 +143,7 @@ export function isUnknownError(error) { * Validate that the user has specified either a database or a secret. * This check is not required for commands that can operate at a * "root" level. + * * @param {object} argv * @param {string} argv.database - The database to use * @param {string} argv.secret - The secret to use @@ -151,9 +152,10 @@ export function isUnknownError(error) { export const validateDatabaseOrSecret = (argv) => { if (!argv.database && !argv.secret && !argv.local) { throw new ValidationError( - "No database or secret specified. Pass either --database, or --secret, or --local.", + "No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.", ); } + return true; }; // used for queries customers can configure diff --git a/test/database/create.mjs b/test/database/create.mjs index e5d79dd7..bba25aca 100644 --- a/test/database/create.mjs +++ b/test/database/create.mjs @@ -31,7 +31,7 @@ describe("database create", () => { { command: "database create --name 'testdb'", message: - "No secret or database provided. Please use either --secret or --database.", + "No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.", }, ].forEach(({ command, message }) => { it(`validates invalid arguments: ${command}`, async () => { diff --git a/test/database/delete.mjs b/test/database/delete.mjs index 887bbc59..2d715867 100644 --- a/test/database/delete.mjs +++ b/test/database/delete.mjs @@ -31,7 +31,7 @@ describe("database delete", () => { { command: "database delete --name 'testdb'", message: - "No secret or database provided. Please use either --secret or --database.", + "No database or secret specified. Please use either --database, --secret, or --local to connect to your desired Fauna database.", }, ].forEach(({ command, message }) => { it(`validates invalid arguments: ${command}`, async () => {