Skip to content

Commit

Permalink
Store a ShellConfig in FaunaCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv committed Sep 18, 2023
1 parent c2a0ae6 commit e346896
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
24 changes: 0 additions & 24 deletions src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,6 @@ export class Config {
}
}

/**
* Builds the options provided to the faunajs client.
* Tries to load the ~/.fauna-shell file and read the default endpoint from there.
*
* Assumes that if the file exists, it would have been created by fauna-shell,
* therefore it would have a defined endpoint.
*
* Flags like --host, --port, etc., provided by the CLI take precedence over what's
* stored in ~/.fauna-shell.
*
* The --endpoint flag overries the default endpoint from fauna-shell.
*
* If ~/.fauna-shell doesn't exist, tries to build the connection options from the
* flags passed to the script.
*
* It always expect a secret key to be set in ~/.fauna-shell or provided via CLI
* arguments.
*
* TODO: Remove and store a ShellConfig in `fauna-command`
*/
export const lookupEndpoint = (flags: any, scope: string, role: string) => {
return ShellConfig.read(flags).lookupEndpoint({ scope, role });
};

export type ShellOpts = {
flags?: { [key: string]: any };
rootConfig?: { [key: string]: any };
Expand Down
9 changes: 5 additions & 4 deletions src/lib/fauna-command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Command, Flags } = require("@oclif/core");
const { lookupEndpoint } = require("../lib/config/index.ts");
const { ShellConfig } = require("../lib/config/index.ts");
const { stringifyEndpoint } = require("../lib/misc.js");
const faunadb = require("faunadb");
const chalk = require("chalk");
Expand Down Expand Up @@ -32,6 +32,7 @@ class FaunaCommand extends Command {
const { flags: f, args: a } = await this.parse(this.constructor);
this.flags = f;
this.args = a;
this.config = ShellConfig.read(this.flags);
}

success(msg) {
Expand All @@ -54,7 +55,7 @@ class FaunaCommand extends Command {
async withClient(f, dbScope, role) {
let connectionOptions;
try {
connectionOptions = lookupEndpoint(this.flags, dbScope, role);
connectionOptions = this.config.lookupEndpoint({ scope: dbScope, role });

const { hostname, port, protocol } = new URL(connectionOptions.url);

Expand Down Expand Up @@ -95,7 +96,7 @@ class FaunaCommand extends Command {
// construct v4 client
let connectionOptions;
try {
connectionOptions = lookupEndpoint(this.flags, dbScope, role);
connectionOptions = this.config.lookupEndpoint({ scope: dbScope, role });

const { hostname, port, protocol } = new URL(connectionOptions.url);

Expand Down Expand Up @@ -130,7 +131,7 @@ class FaunaCommand extends Command {
// construct v10 client
let connectionOptions;
try {
connectionOptions = lookupEndpoint(this.flags, dbScope, role);
connectionOptions = this.config.lookupEndpoint({ scope: dbScope, role });
const client = new FaunaClient(
connectionOptions.url,
connectionOptions.secret,
Expand Down

0 comments on commit e346896

Please sign in to comment.