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

don't blow up for invalid config #302

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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/commands/stack/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class AddStackCommand extends Command {
];

async run() {
const config = ShellConfig.read({});
const config = ShellConfig.read({}, this);

await this.execute(config);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/stack/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class ListStackCommand extends Command {
static examples = ["$ fauna stack list"];

async run() {
const config = ShellConfig.read({});
const config = ShellConfig.read({}, this);

await this.execute(config);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/stack/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class SelectStackCommand extends Command {
static examples = ["$ fauna stack select my-stack"];

async run() {
const config = ShellConfig.read({});
const config = ShellConfig.read({}, this);

await this.execute(config);
}
Expand Down
34 changes: 32 additions & 2 deletions src/lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ export type EndpointConfig = {
graphqlPort: number;
};

export interface LogChannel {
warn(_: string): void;
log(_: string): void;
}

export class ShellConfig {
// fields from CLI and files
flags: Config;
Expand All @@ -144,14 +149,14 @@ export class ShellConfig {
// The path to the project config.
projectPath: string | undefined;

static read(flags: any) {
static read(flags: any, log?: LogChannel) {
const rootConfig = ini.parse(readFileOpt(getRootConfigPath()));
const projectConfigPath = getProjectConfigPath();
const projectConfig = projectConfigPath
? ini.parse(readFile(projectConfigPath))
: undefined;

return new ShellConfig({
const shellConfig = new ShellConfig({
flags,
rootConfig,
projectPath:
Expand All @@ -160,6 +165,12 @@ export class ShellConfig {
: undefined,
projectConfig,
});

if (log !== undefined) {
shellConfig.configErrors().forEach((err) => log.warn(err));
}

return shellConfig;
}

static readWithOverrides(opts?: ShellOpts): ShellConfig {
Expand Down Expand Up @@ -284,6 +295,18 @@ export class ShellConfig {
return this.endpoint!.makeScopedEndpoint(database, opts.role);
};

configErrors(): string[] {
if (this.rootConfig.invalidEndpoints.length > 0) {
return [
`The following endpoint definitions in ${getRootConfigPath()} are invalid:\n ${this.rootConfig.invalidEndpoints.join(
"\n"
)}\n Resolve them by ensuring they have a secret defined or remove them if they are not needed.`,
];
} else {
return [];
}
}

/**
* Saves the project config, if present.
*/
Expand All @@ -295,6 +318,13 @@ export class ShellConfig {
* Saves the root config.
*/
saveRootConfig() {
if (this.rootConfig.invalidEndpoints.length > 0) {
throw new Error(
`The following endpoint definitions in ${getRootConfigPath()} are invalid:\n ${this.rootConfig.invalidEndpoints.join(
"\n"
)}\n Resolve them by ensuring they have a secret defined or remove them if they are not needed.`
);
}
this.rootConfig.save(getRootConfigPath());
}

Expand Down
36 changes: 26 additions & 10 deletions src/lib/config/root-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const ini = require("ini");
export class RootConfig {
defaultEndpoint?: string;
endpoints: { [key: string]: Endpoint };
invalidEndpoints: string[];

constructor(config: Config) {
this.defaultEndpoint = config.strOpt("default");
Expand All @@ -23,14 +24,24 @@ export class RootConfig {
this.endpoints = Object.fromEntries<Endpoint>(
config
.objectsIn("endpoint")
.map(([k, v]) => [k, Endpoint.fromConfig(k, v)])
.filter(([k, v]) => Endpoint.fromConfig(k, v) !== undefined)
.map(([k, v]) => [k, Endpoint.fromConfig(k, v)!])
);
this.invalidEndpoints = config
.objectsIn("endpoint")
.filter(([k, v]) => Endpoint.fromConfig(k, v) === undefined)
.map(([k, _]) => k);
} else {
this.endpoints = Object.fromEntries<Endpoint>(
config
.allObjectsWhere((k) => k !== "default")
.map(([k, v]) => [k, Endpoint.fromConfig(k, v)])
.filter(([k, v]) => Endpoint.fromConfig(k, v) !== undefined)
.map(([k, v]) => [k, Endpoint.fromConfig(k, v)!])
);
this.invalidEndpoints = config
.allObjectsWhere((k) => k !== "default")
.filter(([k, v]) => Endpoint.fromConfig(k, v) === undefined)
.map(([k, _]) => k);
}

if (this.defaultEndpoint === "default") {
Expand Down Expand Up @@ -108,15 +119,20 @@ export class Endpoint {
graphqlHost: string;
graphqlPort: number;

static fromConfig(name: string, config: Config) {
return new Endpoint({
name: name,
secret: config.str("secret"),
url: Endpoint.getURLFromConfig(config),
static fromConfig(name: string, config: Config): Endpoint | undefined {
const secOpt = config.strOpt("secret");
if (secOpt === undefined) {
return undefined;
} else {
return new Endpoint({
name: name,
secret: secOpt,
url: Endpoint.getURLFromConfig(config),

graphqlHost: config.strOpt("graphqlHost"),
graphqlPort: config.numberOpt("graphqlPort"),
});
graphqlHost: config.strOpt("graphqlHost"),
graphqlPort: config.numberOpt("graphqlPort"),
});
}
}

constructor(opts: {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/fauna-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class FaunaCommand extends Command {
const { flags: f, args: a } = await this.parse(this.constructor);
this.flags = f;
this.args = a;
this.shellConfig = ShellConfig.read(this.flags);
this.shellConfig = ShellConfig.read(this.flags, this);
}

success(msg) {
Expand Down
4 changes: 4 additions & 0 deletions test/commands/endpoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe("endpoint:add", () => {
graphqlPort: 443,
},
},
invalidEndpoints: [],
});
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});
Expand Down Expand Up @@ -122,6 +123,7 @@ describe("endpoint:add", () => {
graphqlPort: 443,
},
},
invalidEndpoints: [],
});
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});
Expand Down Expand Up @@ -191,6 +193,7 @@ describe("endpoint:remove", () => {
graphqlPort: 443,
},
},
invalidEndpoints: [],
});
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});
Expand Down Expand Up @@ -230,6 +233,7 @@ describe("endpoint:remove", () => {
graphqlPort: 443,
},
},
invalidEndpoints: [],
});
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});
Expand Down
18 changes: 18 additions & 0 deletions test/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ShellConfig,
ShellOpts,
getProjectConfigPath,
getRootConfigPath,
} from "../../src/lib/config";
import sinon from "sinon";

Expand Down Expand Up @@ -105,6 +106,23 @@ describe("root config", () => {
url: "https://db.fauna.com",
});
});

it("fails to save if the root config has invalid endpoints", () => {
const invalidConfig = new ShellConfig({
rootConfig: {
default: "my-endpoint",
"my-endpoint": {
secret: "fn1234",
url: "http://localhost:8443",
},
invalidEndpoints: ["invalid-endpoint"],
},
});
const expectedMsg = `The following endpoint definitions in ${getRootConfigPath()} are invalid:\n ${invalidConfig.rootConfig.invalidEndpoints.join(
"\n"
)}\n Resolve them by ensuring they have a secret defined or remove them if they are not needed.`;
expect(() => invalidConfig.saveRootConfig()).to.throw(expectedMsg);
});
});

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