Skip to content

Commit

Permalink
Add fauna stack select
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv committed Sep 28, 2023
1 parent 2aa9cf6 commit a933ab9
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/commands/stack/select.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
87 changes: 87 additions & 0 deletions test/commands/stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
});
});

0 comments on commit a933ab9

Please sign in to comment.