Skip to content

Commit

Permalink
Add --endpointURL flag (#261)
Browse files Browse the repository at this point in the history
* Deprecate and hide the old domain/port/scheme flags

* Update config parsing to handle the --endpointURL flag

* Update tests to use --endpointURL, and add a test for the legacy flags

* Add comments and rename 'getURLInner'
  • Loading branch information
macmv authored Sep 28, 2023
1 parent 5b2366b commit f860d85
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class ShellConfig {

this.projectConfig?.validate(this.rootConfig);

const urlFlag = Endpoint.getURLFromConfig(this.flags);
const urlFlag = Endpoint.getURLFromFlags(this.flags);
if (urlFlag !== undefined) {
try {
// eslint-disable-next-line no-new
Expand Down
51 changes: 40 additions & 11 deletions src/lib/config/root-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,51 @@ export class Endpoint {
};
}

/**
* Gets a database URL from config.
*/
static getURLFromConfig = (config: Config): string | undefined => {
const url = config.strOpt("url");
const scheme = config.strOpt("scheme");
const domain = config.strOpt("domain");
const port = config.numberOpt("port");
return this.getURL({
url: config.strOpt("url"),
scheme: config.strOpt("scheme"),
domain: config.strOpt("domain"),
port: config.numberOpt("port"),
});
};

/**
* Gets a database URL from command line flags.
*
* Note: this is similar to `getURLFromConfig`, but looks up `endpointURL`
* instead of `url` for the url value.
*/
static getURLFromFlags = (flags: Config): string | undefined => {
return this.getURL({
url: flags.strOpt("endpointURL"),
scheme: flags.strOpt("scheme"),
domain: flags.strOpt("domain"),
port: flags.numberOpt("port"),
});
};

static getURL = (opts: {
url?: string;
scheme?: string;
domain?: string;
port?: number;
}): string | undefined => {
if (
url === undefined &&
(domain !== undefined || port !== undefined || scheme !== undefined)
opts.url === undefined &&
(opts.domain !== undefined ||
opts.port !== undefined ||
opts.scheme !== undefined)
) {
const scheme0 = scheme ?? "https";
const domain0 = domain ?? "db.fauna.com";
const port0 = port ? `:${port}` : "";
return `${scheme0}://${domain0}${port0}`;
const scheme = opts.scheme ?? "https";
const domain = opts.domain ?? "db.fauna.com";
const port = opts.port ? `:${opts.port}` : "";
return `${scheme}://${domain}${port}`;
} else {
return url;
return opts.url;
}
};
}
15 changes: 13 additions & 2 deletions src/lib/fauna-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,22 +208,33 @@ FaunaCommand.flags = {
...Command.flags,
domain: Flags.string({
description: "Fauna server domain",
// Emits a warning if this flag is used.
deprecated: { to: "endpointURL" },
// Hides the flag in `--help`
hidden: true,
}),
scheme: Flags.string({
description: "Connection scheme",
options: ["https", "http"],
deprecated: { to: "endpointURL" },
hidden: true,
}),
port: Flags.string({
description: "Connection port",
deprecated: { to: "endpointURL" },
hidden: true,
}),
endpointURL: Flags.string({
description: "Database URL. Overrides the `url` in ~/.fauna-shell",
}),
timeout: Flags.string({
description: "Connection timeout in milliseconds",
}),
secret: Flags.string({
description: "Fauna secret key",
description: "Secret key. Overrides the `secret` in ~/.fauna-shell",
}),
endpoint: Flags.string({
description: "Fauna server endpoint",
description: "Connection endpoint, from ~/.fauna-shell",
}),
};

Expand Down
19 changes: 18 additions & 1 deletion test/commands/eval.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect, test } = require("@oclif/test");
const { withOpts, getEndpoint, matchFqlReq } = require("../helpers/utils.js");
const { withOpts, getEndpoint, matchFqlReq, withLegacyOpts } = require("../helpers/utils.js");
const { query: q } = require("faunadb");

describe("eval", () => {
Expand All @@ -20,6 +20,23 @@ describe("eval", () => {
expect(JSON.parse(ctx.stdout).data[0].targetDb).to.equal("root");
});

test
.nock(getEndpoint(), { allowUnmocked: true }, mockQuery)
.stdout()
.command(
withLegacyOpts([
"eval",
"--version",
"4",
"--format",
"json",
"Paginate(Collections())",
])
)
.it("works with legacy --domain, --schema, and --port opts", (ctx) => {
expect(JSON.parse(ctx.stdout).data[0].targetDb).to.equal("root");
});

test
.nock(getEndpoint(), { allowUnmocked: true }, (api) => {
api
Expand Down
18 changes: 17 additions & 1 deletion test/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ const url = require("url");
const { query: q } = require("faunadb");
const env = process.env;

module.exports.withOpts = (cmd) => {
/**
* Sets --domain, --secret, --scheme, and --port.
*/
module.exports.withLegacyOpts = (cmd) => {
const opts = [
"--secret",
env.FAUNA_SECRET,
Expand All @@ -16,6 +19,19 @@ module.exports.withOpts = (cmd) => {
return cmd.concat(opts);
};

/**
* Sets --secret and --endpointURL
*/
module.exports.withOpts = (cmd) => {
const opts = [
"--secret",
env.FAUNA_SECRET,
"--endpointURL",
`${env.FAUNA_SCHEME}://${env.FAUNA_DOMAIN}:${env.FAUNA_PORT}`,
];
return cmd.concat(opts);
};

module.exports.getEndpoint = () =>
url.format({
protocol: env.FAUNA_SCHEME,
Expand Down

0 comments on commit f860d85

Please sign in to comment.