Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --endpointURL flag #261

Merged
merged 4 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,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 {
new URL(urlFlag);
Expand Down
42 changes: 31 additions & 11 deletions src/lib/config/root-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,41 @@ export class Endpoint {
}

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.getURLInner({
url: config.strOpt("url"),
scheme: config.strOpt("scheme"),
domain: config.strOpt("domain"),
port: config.numberOpt("port"),
});
};

static getURLFromFlags = (flags: Config): string | undefined => {
macmv marked this conversation as resolved.
Show resolved Hide resolved
return this.getURLInner({
url: flags.strOpt("endpointURL"),
scheme: flags.strOpt("scheme"),
domain: flags.strOpt("domain"),
port: flags.numberOpt("port"),
});
};

static getURLInner = (opts: {
macmv marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -206,22 +206,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",
}),
graphqlHost: Flags.string({
description: "The Fauna GraphQL API host",
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