Skip to content

Commit

Permalink
Pass role and scope in to lookupEndpoint instead of ShellConfig (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv authored Sep 27, 2023
1 parent 8e849c4 commit 3e95c5c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
28 changes: 9 additions & 19 deletions src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,13 @@ export class Config {
* TODO: Remove and store a ShellConfig in `fauna-command`
*/
export const lookupEndpoint = (flags: any, scope: string, role: string) => {
return ShellConfig.read(flags, scope, role).lookupEndpoint();
return ShellConfig.read(flags).lookupEndpoint({ scope, role });
};

export type ShellOpts = {
flags?: { [key: string]: any };
rootConfig?: { [key: string]: any };
projectConfig?: { [key: string]: any };
scope?: string;
role?: string;
};

export type EndpointConfig = {
Expand All @@ -143,17 +141,13 @@ export class ShellConfig {
flags: Config;
rootConfig: RootConfig;
projectConfig: ProjectConfig | undefined;
args: {
scope?: string;
role?: string;
};

// The selected stack from the project config. If there is a project config, this will also be set.
stack: Stack | undefined;
// The fully configured endpoint, including command line flags that override things like the URL.
endpoint: Endpoint;

static read(flags: any, scope: string, role: string) {
static read(flags: any) {
const rootConfig = ini.parse(readFileOpt(getRootConfigPath()));
const projectConfigPath = getProjectConfigPath();
const projectConfig = projectConfigPath
Expand All @@ -164,8 +158,6 @@ export class ShellConfig {
flags,
rootConfig,
projectConfig,
scope,
role,
});
}

Expand All @@ -181,11 +173,6 @@ export class ShellConfig {

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

this.args = {
scope: opts.scope,
role: opts.role,
};

const urlFlag = Endpoint.getURLFromConfig(this.flags);
if (urlFlag !== undefined) {
try {
Expand Down Expand Up @@ -258,16 +245,19 @@ export class ShellConfig {
}
}

lookupEndpoint = (): EndpointConfig => {
lookupEndpoint = (opts: {
scope?: string;
role?: string;
}): EndpointConfig => {
let database = this.stack?.database ?? "";
if (this.args.scope !== undefined) {
if (opts.scope !== undefined) {
if (this.stack !== undefined) {
database += "/";
}
database += this.args.scope;
database += opts.scope;
}

return this.endpoint.makeScopedEndpoint(database, this.args.role);
return this.endpoint.makeScopedEndpoint(database, opts.role);
};
}

Expand Down
9 changes: 7 additions & 2 deletions test/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import {
} from "../../src/lib/config";
import sinon from "sinon";

const lookupEndpoint = (opts: ShellOpts) => {
return new ShellConfig(opts).lookupEndpoint();
const lookupEndpoint = (
opts: ShellOpts & { role?: string; scope?: string }
) => {
return new ShellConfig(opts).lookupEndpoint({
role: opts.role,
scope: opts.scope,
});
};

describe("root config", () => {
Expand Down

0 comments on commit 3e95c5c

Please sign in to comment.