Skip to content

Commit

Permalink
Remove function indirection for common options (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecooper authored Jan 6, 2025
1 parent c29b190 commit 57641e1
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 178 deletions.
2 changes: 1 addition & 1 deletion src/commands/database/create.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
import { ServiceError } from "fauna";

import { container } from "../../cli.mjs";
import { validateDatabaseOrSecret } from "../../lib/command-helpers.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 { validateDatabaseOrSecret } from "../../lib/middleware.mjs";

async function runCreateQuery(secret, argv) {
const { fql } = container.resolve("fauna");
Expand Down
11 changes: 9 additions & 2 deletions src/commands/database/database.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
//@ts-check

import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import {
ACCOUNT_OPTIONS,
CORE_OPTIONS,
DATABASE_PATH_OPTIONS,
} from "../../lib/options.mjs";
import createCommand from "./create.mjs";
import deleteCommand from "./delete.mjs";
import listCommand from "./list.mjs";

function buildDatabase(yargs) {
return yargsWithCommonQueryOptions(yargs)
return yargs
.options(ACCOUNT_OPTIONS)
.options(CORE_OPTIONS)
.options(DATABASE_PATH_OPTIONS)
.command(listCommand)
.command(createCommand)
.command(deleteCommand)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/database/delete.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import { ServiceError } from "fauna";

import { container } from "../../cli.mjs";
import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs";
import { CommandError } from "../../lib/errors.mjs";
import { faunaToCommandError } from "../../lib/fauna.mjs";
import { getSecret, retryInvalidCredsOnce } from "../../lib/fauna-client.mjs";
import { validateDatabaseOrSecret } from "../../lib/middleware.mjs";

async function runDeleteQuery(secret, argv) {
const { fql } = container.resolve("fauna");
Expand Down
43 changes: 31 additions & 12 deletions src/commands/login.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ts-check

import { container } from "../cli.mjs";
import { yargsWithCommonOptions } from "../lib/command-helpers.mjs";
import { FaunaAccountClient } from "../lib/fauna-account-client.mjs";

async function doLogin(argv) {
Expand Down Expand Up @@ -38,17 +37,37 @@ async function doLogin(argv) {
* @returns
*/
function buildLoginCommand(yargs) {
return yargsWithCommonOptions(yargs, {
user: {
alias: "u",
type: "string",
description: "User to log in as.",
default: "default",
},
}).example([
["$0 login", "Log in as the 'default' user."],
["$0 login --user john_doe", "Log in as the 'john_doe' user."],
]);
return yargs
.options({
"account-url": {
type: "string",
description: "The Fauna account URL to query",
default: "https://account.fauna.com",
hidden: true,
},
"client-id": {
type: "string",
description: "the client id to use when calling Fauna",
required: false,
hidden: true,
},
"client-secret": {
type: "string",
description: "the client secret to use when calling Fauna",
required: false,
hidden: true,
},
user: {
alias: "u",
type: "string",
description: "User to log in as.",
default: "default",
},
})
.example([
["$0 login", "Log in as the 'default' user."],
["$0 login --user john_doe", "Log in as the 'john_doe' user."],
]);
}

export default {
Expand Down
24 changes: 17 additions & 7 deletions src/commands/query.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
//@ts-check

import { container } from "../cli.mjs";
import {
resolveFormat,
validateDatabaseOrSecret,
yargsWithCommonConfigurableQueryOptions,
} from "../lib/command-helpers.mjs";
import {
CommandError,
isUnknownError,
Expand All @@ -16,7 +11,17 @@ import {
formatQueryResponse,
getSecret,
} from "../lib/fauna-client.mjs";
import { isTTY } from "../lib/misc.mjs";
import {
resolveIncludeOptions,
validateDatabaseOrSecret,
} from "../lib/middleware.mjs";
import {
ACCOUNT_OPTIONS,
CORE_OPTIONS,
DATABASE_PATH_OPTIONS,
QUERY_OPTIONS,
} from "../lib/options.mjs";
import { isTTY, resolveFormat } from "../lib/utils.mjs";

function validate(argv) {
const { existsSync, accessSync, constants } = container.resolve("fs");
Expand Down Expand Up @@ -149,7 +154,12 @@ async function queryCommand(argv) {
}

function buildQueryCommand(yargs) {
return yargsWithCommonConfigurableQueryOptions(yargs)
return yargs
.options(ACCOUNT_OPTIONS)
.options(DATABASE_PATH_OPTIONS)
.options(CORE_OPTIONS)
.options(QUERY_OPTIONS)
.middleware(resolveIncludeOptions)
.positional("fql", {
type: "string",
description: "FQL query to run. Use - to read from stdin.",
Expand Down
3 changes: 1 addition & 2 deletions src/commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ts-check

import { container } from "../../cli.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { CommandError } from "../../lib/errors.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";

Expand Down Expand Up @@ -65,7 +64,7 @@ async function doAbandon(argv) {
}

function buildAbandonCommand(yargs) {
return yargsWithCommonQueryOptions(yargs)
return yargs
.options({
input: {
description: "Prompt for input. Use --no-input to disable.",
Expand Down
3 changes: 1 addition & 2 deletions src/commands/schema/commit.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//@ts-check

import { container } from "../../cli.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { CommandError } from "../../lib/errors.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";

Expand Down Expand Up @@ -69,7 +68,7 @@ async function doCommit(argv) {
}

function buildCommitCommand(yargs) {
return yargsWithCommonQueryOptions(yargs)
return yargs
.options({
input: {
description: "Prompt for input. Use --no-input to disable.",
Expand Down
7 changes: 3 additions & 4 deletions src/commands/schema/diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import chalk from "chalk";

import { container } from "../../cli.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { ValidationError } from "../../lib/errors.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";
import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";
import { LOCAL_SCHEMA_OPTIONS } from "./schema.mjs";

/**
* @returns {[string, string]} An tuple containing the source and target schema
Expand Down Expand Up @@ -110,7 +109,8 @@ async function doDiff(argv) {
}

function buildDiffCommand(yargs) {
return yargsWithCommonQueryOptions(yargs)
return yargs
.options(LOCAL_SCHEMA_OPTIONS)
.options({
staged: {
description: "Show the diff between the active and staged schema.",
Expand All @@ -128,7 +128,6 @@ function buildDiffCommand(yargs) {
default: false,
type: "boolean",
},
...localSchemaOptions,
})
.example([
[
Expand Down
7 changes: 3 additions & 4 deletions src/commands/schema/pull.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//@ts-check

import { container } from "../../cli.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";
import { localSchemaOptions } from "./schema.mjs";
import { LOCAL_SCHEMA_OPTIONS } from "./schema.mjs";

async function determineFileState(argv, filenames) {
const gatherFSL = container.resolve("gatherFSL");
Expand Down Expand Up @@ -135,7 +134,8 @@ async function doPull(argv) {
}

function buildPullCommand(yargs) {
return yargsWithCommonQueryOptions(yargs)
return yargs
.options(LOCAL_SCHEMA_OPTIONS)
.options({
delete: {
description:
Expand All @@ -148,7 +148,6 @@ function buildPullCommand(yargs) {
type: "boolean",
default: false,
},
...localSchemaOptions,
})
.example([
[
Expand Down
7 changes: 3 additions & 4 deletions src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import path from "path";

import { container } from "../../cli.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { ValidationError } from "../../lib/errors.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";
import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";
import { LOCAL_SCHEMA_OPTIONS } from "./schema.mjs";

/**
* Pushes a schema (FSL) based on argv.
Expand Down Expand Up @@ -98,7 +97,8 @@ export async function pushSchema(argv) {
}

function buildPushCommand(yargs) {
return yargsWithCommonQueryOptions(yargs)
return yargs
.options(LOCAL_SCHEMA_OPTIONS)
.options({
input: {
description: "Prompt for input. Use --no-input to disable.",
Expand All @@ -111,7 +111,6 @@ function buildPushCommand(yargs) {
type: "boolean",
default: false,
},
...localSchemaOptions,
})
.example([
[
Expand Down
12 changes: 10 additions & 2 deletions src/commands/schema/schema.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
//@ts-check

import { validateDatabaseOrSecret } from "../../lib/command-helpers.mjs";
import { validateDatabaseOrSecret } from "../../lib/middleware.mjs";
import {
ACCOUNT_OPTIONS,
CORE_OPTIONS,
DATABASE_PATH_OPTIONS,
} from "../../lib/options.mjs";
import abandonCommand from "./abandon.mjs";
import commitCommand from "./commit.mjs";
import diffCommand from "./diff.mjs";
import pullCommand from "./pull.mjs";
import pushCommand from "./push.mjs";
import statusCommand from "./status.mjs";

export const localSchemaOptions = {
export const LOCAL_SCHEMA_OPTIONS = {
"fsl-directory": {
alias: ["directory", "dir"],
type: "string",
Expand All @@ -20,6 +25,9 @@ export const localSchemaOptions = {

function buildSchema(yargs) {
return yargs
.options(ACCOUNT_OPTIONS)
.options(DATABASE_PATH_OPTIONS)
.options(CORE_OPTIONS)
.command(abandonCommand)
.command(commitCommand)
.command(diffCommand)
Expand Down
25 changes: 11 additions & 14 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import chalk from "chalk";
import path from "path";

import { container } from "../../cli.mjs";
import { yargsWithCommonQueryOptions } from "../../lib/command-helpers.mjs";
import { CommandError } from "../../lib/errors.mjs";
import { getSecret } from "../../lib/fauna-client.mjs";
import { reformatFSL } from "../../lib/schema.mjs";
import { localSchemaOptions } from "./schema.mjs";
import { LOCAL_SCHEMA_OPTIONS } from "./schema.mjs";

async function doStatus(argv) {
const logger = container.resolve("logger");
Expand Down Expand Up @@ -81,18 +80,16 @@ async function doStatus(argv) {
}

function buildStatusCommand(yargs) {
return yargsWithCommonQueryOptions(yargs)
.options(localSchemaOptions)
.example([
[
"$0 schema status --database us/my_db",
"Get the staged schema status for the 'us/my_db' database.",
],
[
"$0 schema status --secret my-secret",
"Get the staged schema status for the database scoped to a secret.",
],
]);
return yargs.options(LOCAL_SCHEMA_OPTIONS).example([
[
"$0 schema status --database us/my_db",
"Get the staged schema status for the 'us/my_db' database.",
],
[
"$0 schema status --secret my-secret",
"Get the staged schema status for the database scoped to a secret.",
],
]);
}

export default {
Expand Down
21 changes: 14 additions & 7 deletions src/commands/shell.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import repl from "node:repl";
import * as esprima from "esprima";

import { container } from "../cli.mjs";
import {
QUERY_INFO_CHOICES,
resolveFormat,
validateDatabaseOrSecret,
yargsWithCommonConfigurableQueryOptions,
} from "../lib/command-helpers.mjs";
import { formatQueryResponse, getSecret } from "../lib/fauna-client.mjs";
import { clearHistoryStorage, initHistoryStorage } from "../lib/file-util.mjs";
import { validateDatabaseOrSecret } from "../lib/middleware.mjs";
import {
ACCOUNT_OPTIONS,
CORE_OPTIONS,
DATABASE_PATH_OPTIONS,
QUERY_INFO_CHOICES,
QUERY_OPTIONS,
} from "../lib/options.mjs";
import { resolveFormat } from "../lib/utils.mjs";

async function shellCommand(argv) {
const { query: v4Query } = container.resolve("faunadb");
Expand Down Expand Up @@ -219,7 +222,11 @@ async function buildCustomEval(argv) {
}

function buildShellCommand(yargs) {
return yargsWithCommonConfigurableQueryOptions(yargs)
return yargs
.options(ACCOUNT_OPTIONS)
.options(DATABASE_PATH_OPTIONS)
.options(CORE_OPTIONS)
.options(QUERY_OPTIONS)
.example([
[
"$0 shell --database us/my_db",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/formatting/codeToAnsi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import log from "shiki/langs/log.mjs";
import yaml from "shiki/langs/yaml.mjs";
import githubDarkHighContrast from "shiki/themes/github-dark-high-contrast.mjs";

import { isTTY } from "../misc.mjs";
import { isTTY } from "../utils.mjs";
import { fql } from "./fql.mjs";

const THEME = "github-dark-high-contrast";
Expand Down
Loading

0 comments on commit 57641e1

Please sign in to comment.