Skip to content

Commit

Permalink
address PR feedback
Browse files Browse the repository at this point in the history
- support colors on all calls to /validate and /status
- fix schema push --active flag description
- fix schema status confirmation prompts
  • Loading branch information
echo-bravo-yahoo committed Oct 29, 2024
1 parent 3199c26 commit 665db93
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 61 deletions.
9 changes: 3 additions & 6 deletions src/commands/schema/abandon.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ async function doAbandon(argv) {
});

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/abandon?${params}`,
secret: argv.secret,
method: "POST",
});
logger.stdout("Schema has been abandoned");
Expand All @@ -26,9 +25,8 @@ async function doAbandon(argv) {
if (argv.color) params.set("color", "ansi");

const response = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/status?${params}`,
secret: argv.secret,
method: "GET",
});

Expand All @@ -46,9 +44,8 @@ async function doAbandon(argv) {
const params = new URLSearchParams({ version: response.version });

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/abandon?${params}`,
secret: argv.secret,
method: "POST",
});

Expand Down
9 changes: 3 additions & 6 deletions src/commands/schema/commit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ async function doCommit(argv) {
});

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/commit?${params}`,
secret: argv.secret,
method: "POST",
});

Expand All @@ -27,9 +26,8 @@ async function doCommit(argv) {
if (argv.color) params.set("color", "ansi");

const response = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/status?${params}`,
secret: argv.secret,
method: "GET",
});

Expand All @@ -50,9 +48,8 @@ async function doCommit(argv) {
const params = new URLSearchParams({ version: response.version });

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: `/schema/1/staged/commit?${params}`,
secret: argv.secret,
method: "POST",
});

Expand Down
6 changes: 2 additions & 4 deletions src/commands/schema/diff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ async function doDiff(argv) {
if (target === "staged") params.set("diff", diffKind);

const { version, status, diff } = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/staged/status",
params,
secret: argv.secret,
method: "GET",
});

Expand All @@ -71,10 +70,9 @@ async function doDiff(argv) {
}

const { diff } = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/validate",
params,
secret: argv.secret,
body: files,
method: "POST",
});
Expand Down
11 changes: 3 additions & 8 deletions src/commands/schema/pull.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ async function doPull(argv) {

// fetch the list of remote FSL files
const filesResponse = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/files",
method: "GET",
secret: argv.secret,
});

// check if there's a staged schema
const statusResponse = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/staged/status",
params: new URLSearchParams({ version: filesResponse.version }),
method: "GET",
secret: argv.secret,
});

// if there's a staged schema, cannot use the --active flag.
Expand Down Expand Up @@ -88,10 +86,7 @@ async function doPull(argv) {
const getAllSchemaFileContents = container.resolve(
"getAllSchemaFileContents",
);
const contents = await getAllSchemaFileContents(filenames, {
secret: argv.secret,
baseUrl: argv.url,
});
const contents = await getAllSchemaFileContents(filenames, argv);

// don't start writing or deleting files until we've successfully fetched all
// the remote schema files
Expand Down
21 changes: 12 additions & 9 deletions src/commands/schema/push.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ async function doPush(argv) {
const gatherFSL = container.resolve("gatherFSL");
const fsl = reformatFSL(await gatherFSL(argv.dir));

const isStagedPush = !argv.active;

if (!argv.input) {
const params = new URLSearchParams({
force: "true",
staged: argv.active ? "false" : "true",
});

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/update",
params,
body: fsl,
secret: argv.secret,
method: "POST",
});
} else {
Expand All @@ -35,21 +36,24 @@ async function doPush(argv) {
if (argv.color) params.set("color", "ansi");

const response = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/validate",
params,
body: fsl,
secret: argv.secret,
method: "POST",
});

let message = "Accept and push changes?";
let message = isStagedPush
? "Stage the above changes?"
: "Push the above changes?";
if (response.diff) {
logger.stdout(`Proposed diff:\n`);
logger.stdout(response.diff);
} else {
logger.stdout("No logical changes.");
message = "Push file contents anyway?";
message = isStagedPush
? "Stage the file contents anyway?"
: "Push the file contents anyway?";
}
const confirm = container.resolve("confirm");
const confirmed = await confirm({
Expand All @@ -64,11 +68,10 @@ async function doPush(argv) {
});

await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/update",
params,
body: fsl,
secret: argv.secret,
method: "POST",
});
} else {
Expand All @@ -87,7 +90,7 @@ function buildPushCommand(yargs) {
},
active: {
description:
"Stages the schema change instead of applying it immediately",
"Immediately applies the schema change instead of staging it",
type: "boolean",
default: false,
},
Expand Down
7 changes: 3 additions & 4 deletions src/commands/schema/status.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ async function doStatus(argv) {
const fsl = reformatFSL(await gatherFSL(argv.dir));

const statusResponse = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/staged/status",
params,
secret: argv.secret,
method: "GET",
});

Expand All @@ -28,11 +27,11 @@ async function doStatus(argv) {
staged: "true",
version: statusResponse.version,
});
if (argv.color) params.set("color", "ansi");
const validationResponse = await makeFaunaRequest({
baseUrl: argv.url,
argv,
path: "/schema/1/validate",
params,
secret: argv.secret,
method: "POST",
body: fsl,
});
Expand Down
17 changes: 9 additions & 8 deletions src/lib/db.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { container } from "../cli.mjs";

/**
* @typedef {Object} fetchParameters
* @property {string} secret - The secret to include in the AUTHORIZATION header of the request.
* @property {string} baseUrl - The base URL from the scheme up through the top level domain and optional port; defaults to "https://db.fauna.com:443".
* @property {Object} argv - The parsed argv from yargs; used to find the base url (`argv.url`), secret (`argv.secret`), and color support (`argv.color`). To overwrite, provided a modified argv to `makeFaunaRequest`.
* @property {string} path - The path part of the URL. Added to the baseUrl and params to build the full URL.
* @property {URLSearchParams} [params] - The parameters (and their values) to set in the query string.
* @property {URLSearchParams|undefined} [params] - The parameters (and their values) to set in the query string.
* @property {method} method - The HTTP method to use when making the request.
* @property {object} [body] - The body to include in the request.
* @property {boolean} [shouldThrow=true] - Whether or not to throw if the network request succeeds but is not a 2XX. If this is set to false, makeFaunaRequest will return the error instead of throwing.
Expand All @@ -21,31 +20,33 @@ import { container } from "../cli.mjs";
* @param {fetchParameters} args
*/
export async function makeFaunaRequest({
secret,
baseUrl,
argv,
path,
params = undefined,
method,
body = undefined,
shouldThrow = true,
}) {
const fetch = container.resolve("fetch");
const routesWithColor = ["/schema/1/staged/status", "/schema/1/validate"];
if (params && argv.color && routesWithColor.includes(path))
params.set("color", "ansi");
if (params && params.sort) params.sort();
const paramsString = params && params.size ? `?${params.toString()}` : "";
let fullUrl;

try {
fullUrl = new URL(`${path}${paramsString}`, baseUrl).href;
fullUrl = new URL(`${path}${paramsString}`, argv.url).href;
} catch (e) {
e.message = `Could not build valid URL out of base url (${baseUrl}), path (${path}), and params string (${paramsString}) built from params (${JSON.stringify(
e.message = `Could not build valid URL out of base url (${argv.url}), path (${path}), and params string (${paramsString}) built from params (${JSON.stringify(
params,
)}).`;
throw e;
}

const fetchArgs = {
method,
headers: { AUTHORIZATION: `Bearer ${secret}` },
headers: { AUTHORIZATION: `Bearer ${argv.secret}` },
};

if (body) fetchArgs.body = body;
Expand Down
7 changes: 3 additions & 4 deletions src/lib/schema.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,17 @@ export async function writeSchemaFiles(dir, filenameToContentsDict) {

/**
* @param {string[]} filenames - A list of schema file names to fetch
* @param {Omit<fetchParameters, "path"|"method">} overrides
* @param {object} argv
* @returns {Promise<Record<string, string>>} A map of schema file names to their contents.
*/
export async function getAllSchemaFileContents(filenames, { ...overrides }) {
export async function getAllSchemaFileContents(filenames, argv) {
const promises = [];
/** @type Record<string, string> */
const fileContentCollection = {};
for (const filename of filenames) {
promises.push(
makeFaunaRequest({
baseUrl: overrides.baseUrl,
secret: overrides.secret,
argv,
path: `/schema/1/files/${encodeURIComponent(filename)}`,
method: "GET",
}).then(({ content }) => {
Expand Down
12 changes: 11 additions & 1 deletion test/general-cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { expect } from "chai";
import chalk from "chalk";
import { stub } from "sinon";

import { f } from "../helpers.mjs";
import { builtYargs, run } from "../src/cli.mjs";
import { setupTestContainer as setupContainer } from "../src/config/setup-test-container.mjs";
import { f } from "./helpers.mjs";

describe("cli operations", function () {
let container;
Expand Down Expand Up @@ -84,7 +84,17 @@ describe("cli operations", function () {
});

it("should check for updates when run", async function () {
const fetch = container.resolve("fetch");
fetch.onCall(0).resolves(
f({
version: 0,
status: "none",
diff: "Staged schema: none",
pending_summary: "",
text_diff: "",
}),
);
fetch.onCall(1).resolves(
f({
version: 0,
diff: "",
Expand Down
5 changes: 4 additions & 1 deletion test/schema/pull.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ describe("schema pull", function () {
);
// the version param in the URL is important - we use it for optimistic locking
expect(fetch).to.have.been.calledWith(
buildUrl("/schema/1/staged/status", { version: "194838274939473" }),
buildUrl("/schema/1/staged/status", {
version: "194838274939473",
color: "ansi",
}),
commonFetchParams,
);
expect(fetch).to.have.been.calledWith(
Expand Down
Loading

0 comments on commit 665db93

Please sign in to comment.