From a933ab9c047718ccf7028918dba814479cd2563a Mon Sep 17 00:00:00 2001 From: Neil Macneale V Date: Thu, 28 Sep 2023 10:39:08 -0700 Subject: [PATCH] Add fauna stack select --- src/commands/stack/select.ts | 39 ++++++++++++++++ test/commands/stack.test.ts | 87 ++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/commands/stack/select.ts diff --git a/src/commands/stack/select.ts b/src/commands/stack/select.ts new file mode 100644 index 00000000..dbd0fa26 --- /dev/null +++ b/src/commands/stack/select.ts @@ -0,0 +1,39 @@ +import { Args, Command } from "@oclif/core"; +import { ShellConfig } from "../../lib/config"; + +export default class SelectStackCommand extends Command { + static args = { + stack: Args.string({ + description: "The new default stack to use", + required: true, + }), + }; + + static description = "Updates the default stack in `.fauna-project`."; + + static examples = ["$ fauna stack select my-stack"]; + + async run() { + const config = ShellConfig.read({}); + + await this.execute(config); + } + + async execute(config: ShellConfig) { + const { args } = await this.parse(); + + if (config.projectConfig === undefined) { + this.error("No project config found"); + } + + if (!Object.keys(config.projectConfig.stacks).includes(args.stack)) { + this.error( + `Stack ${args.stack} not found in project config. Run \`fauna stack list\` to see available stacks` + ); + } + + config.projectConfig.defaultStack = args.stack; + config.saveProjectConfig(); + console.log(`Selected stack ${args.stack}`); + } +} diff --git a/test/commands/stack.test.ts b/test/commands/stack.test.ts index 4f72b79a..ed21e99b 100644 --- a/test/commands/stack.test.ts +++ b/test/commands/stack.test.ts @@ -3,6 +3,7 @@ import { ShellConfig } from "../../src/lib/config"; import sinon, { SinonStub } from "sinon"; import AddStackCommand from "../../src/commands/stack/add"; import ListStackCommand from "../../src/commands/stack/list"; +import SelectStackCommand from "../../src/commands/stack/select"; import { Config } from "@oclif/core"; const rootConfig = { @@ -241,3 +242,89 @@ describe("stack:list", () => { expect(ctx.config.saveProjectConfig.called).to.be.false; }); }); + +describe("stack:select", () => { + test + .add("config", () => + stubbedProjectConfig({ + default: "my-app", + stack: { + "my-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + "foo-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + }, + }) + ) + .stdout() + .do((ctx) => + new SelectStackCommand(["foo-app"], new Config({} as any)).execute( + ctx.config + ) + ) + .it("selects a stack", (ctx) => { + expect(ctx.stdout).to.equal("Selected stack foo-app\n"); + expect(ctx.config.projectConfig).to.deep.equal({ + defaultStack: "foo-app", + stacks: { + "my-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + "foo-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + }, + }); + expect(ctx.config.saveProjectConfig.calledOnce).to.be.true; + }); + + test + .add("config", () => + stubbedProjectConfig({ + default: "my-app", + stack: { + "my-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + "foo-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + }, + }) + ) + .stdout() + .do((ctx) => + new SelectStackCommand(["baz-app"], new Config({} as any)).execute( + ctx.config + ) + ) + .catch((e) => { + expect(e.message).to.equal( + "Stack baz-app not found in project config. Run `fauna stack list` to see available stacks" + ); + }) + .it("disallows stacks that don't exist", (ctx) => { + expect(ctx.config.projectConfig).to.deep.equal({ + defaultStack: "my-app", + stacks: { + "my-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + "foo-app": { + endpoint: "my-endpoint", + database: "my-db", + }, + }, + }); + expect(ctx.config.saveProjectConfig.called).to.be.false; + }); +});