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

Cleanup endpoint commands #268

Merged
merged 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,18 @@
"@oclif/plugin-help",
"@oclif/plugin-plugins"
],
"topicSeparator": " "
"topicSeparator": " ",
"topics": {
"endpoint": {
"description": "Manage endpoints in ~/.fauna-shell."
},
"project": {
"description": "Manage project settings in .fauna-project."
},
"stack": {
"description": "Manage stacks in the current project."
}
}
},
"repository": "fauna/fauna-shell",
"scripts": {
Expand Down
31 changes: 0 additions & 31 deletions src/commands/delete-endpoint.js

This file was deleted.

27 changes: 11 additions & 16 deletions src/commands/add-endpoint.ts → src/commands/endpoint/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Flags, Args, Command, ux } from "@oclif/core";
import { input, confirm } from "@inquirer/prompts";
import { Endpoint, ShellConfig, getRootConfigPath } from "../lib/config";
import FaunaClient from "../lib/fauna-client";
import { Endpoint, ShellConfig, getRootConfigPath } from "../../lib/config";
import FaunaClient from "../../lib/fauna-client";

export default class AddEndpointCommand extends Command {
static args = {
Expand All @@ -10,14 +10,12 @@ export default class AddEndpointCommand extends Command {
}),
};

static description = `
Adds an endpoint to ~/.fauna-shell.
`;
static description = "Adds an endpoint to ~/.fauna-shell.";

static examples = [
"$ fauna add-endpoint",
"$ fauna add-endpoint localhost --url http://localhost:8443/ --key secret",
"$ fauna add-endpoint localhost --set-default",
"$ fauna endpoint add",
"$ fauna endpoint add localhost --url http://localhost:8443/ --key secret",
"$ fauna endpoint add localhost --set-default",
];

static flags = {
Expand All @@ -38,6 +36,9 @@ Adds an endpoint to ~/.fauna-shell.
}),
};

static aliases = ["add-endpoint"];
static deprecateAliases = true;

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

Expand Down Expand Up @@ -83,13 +84,7 @@ Adds an endpoint to ~/.fauna-shell.
}));

const secret =
flags?.secret ??
(await input({
message: "Database Secret",
validate: async (secret) => {
return true;
},
}));
flags?.secret ?? (await input({ message: "Database Secret" }));

ux.action.start("Checking secret");

Expand All @@ -102,7 +97,7 @@ Adds an endpoint to ~/.fauna-shell.
}
} catch (e) {
ux.action.stop();
console.log("Warning: could not connect to fauna");
console.log("Warning: could not connect to Fauna");
} finally {
await client.close();
}
Expand Down
33 changes: 33 additions & 0 deletions src/commands/endpoint/list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Command } from "@oclif/core";
import { ShellConfig } from "../../lib/config";
import chalk from "chalk";

export default class ListEndpointCommand extends Command {
static flags = {};

static description = "Lists endpoints in ~/.fauna-shell.";
macmv marked this conversation as resolved.
Show resolved Hide resolved

static examples = ["$ fauna endpoint list"];

static aliases = ["list-endpoints"];
static deprecateAliases = true;

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

await this.execute(config);
}

async execute(config: ShellConfig) {
await this.parse();

this.log("Available endpoints:");
for (const key of Object.keys(config.rootConfig.endpoints)) {
if (config.rootConfig.defaultEndpoint === key) {
this.log(chalk.green("* ") + key);
} else {
this.log(" " + key);
}
}
}
}
45 changes: 45 additions & 0 deletions src/commands/endpoint/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Args, Command } from "@oclif/core";
import { ShellConfig } from "../../lib/config";

export default class DeleteEndpointCommand extends Command {
static description = "Deletes an endpoint from ~/.fauna-shell.";
macmv marked this conversation as resolved.
Show resolved Hide resolved

static examples = ["$ fauna endpoint remove my_endpoint"];

static flags = {};

static args = {
name: Args.string({
required: true,
description: "Endpoint name",
}),
};

static aliases = ["delete-endpoint"];
static deprecateAliases = true;

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

await this.execute(config);
}

async execute(config: ShellConfig) {
const { args } = await this.parse();
const name = args.name;

if (config.rootConfig.endpoints[name] === undefined) {
this.error(`No such endpoint ${name}`);
}

// Clear the default if `name` is the default.
if (config.rootConfig.defaultEndpoint === name) {
config.rootConfig.defaultEndpoint = undefined;
}
delete config.rootConfig.endpoints[name];

config.saveRootConfig();

this.log(`Removed endpoint ${name}.`);
}
}
43 changes: 0 additions & 43 deletions src/commands/list-endpoints.js

This file was deleted.

116 changes: 112 additions & 4 deletions test/commands/endpoint.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect, test } from "@oclif/test";
import { ShellConfig, getRootConfigPath } from "../../src/lib/config";
import sinon, { SinonStub } from "sinon";
import AddEndpointCommand from "../../src/commands/add-endpoint";
import AddEndpointCommand from "../../src/commands/endpoint/add";
import ListEndpointCommand from "../../src/commands/endpoint/list";
import RemoveEndpointCommand from "../../src/commands/endpoint/remove";
import { Config } from "@oclif/core";

const rootConfigPath = getRootConfigPath();
Expand All @@ -18,7 +20,7 @@ const stubbedRootConfig = (
return config as any;
};

describe("add-endpoint", () => {
describe("endpoint:add", () => {
test
.add("config", () =>
stubbedRootConfig({
Expand All @@ -45,7 +47,7 @@ describe("add-endpoint", () => {
)
.it("adds an endpoint", (ctx) => {
expect(ctx.stdout).to.equal(
`Saved endpoint foobar to ${rootConfigPath}\n`
`Warning: could not connect to Fauna\nSaved endpoint foobar to ${rootConfigPath}\n`
);
expect(ctx.config.rootConfig).to.deep.equal({
defaultEndpoint: "my-endpoint",
Expand Down Expand Up @@ -96,7 +98,7 @@ describe("add-endpoint", () => {
)
.it("sets default endpoint", (ctx) => {
expect(ctx.stdout).to.equal(
`Saved endpoint foobar to ${rootConfigPath}\n`
`Warning: could not connect to Fauna\nSaved endpoint foobar to ${rootConfigPath}\n`
);
expect(ctx.config.rootConfig).to.deep.equal({
defaultEndpoint: "foobar",
Expand All @@ -120,3 +122,109 @@ describe("add-endpoint", () => {
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});
});

describe("endpoint:list", () => {
test
.add("config", () =>
stubbedRootConfig({
default: "my-endpoint",
"my-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
},
"other-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
},
})
)
.stdout()
.do((ctx) =>
new ListEndpointCommand([], new Config({} as any)).execute(ctx.config)
)
.it("lists endpoints", (ctx) => {
expect(ctx.stdout).to.equal(
`Available endpoints:\n* my-endpoint\n other-endpoint\n`
);
expect(ctx.config.saveRootConfig.calledOnce).to.be.false;
});
});

describe("endpoint:remove", () => {
test
.add("config", () =>
stubbedRootConfig({
default: "my-endpoint",
"my-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
},
"other-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
},
})
)
.stdout()
.do((ctx) =>
new RemoveEndpointCommand(
["other-endpoint"],
new Config({} as any)
).execute(ctx.config)
)
.it("removes an endpoint", (ctx) => {
expect(ctx.stdout).to.equal(`Removed endpoint other-endpoint.\n`);
expect(ctx.config.rootConfig).to.deep.equal({
defaultEndpoint: "my-endpoint",
endpoints: {
"my-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
// These graphql bits are only saved if they differ from the
// default.
graphqlHost: "graphql.fauna.com",
graphqlPort: 443,
},
},
});
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});

test
.add("config", () =>
stubbedRootConfig({
default: "my-endpoint",
"my-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
},
"other-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
},
})
)
.stdout()
.do((ctx) =>
new RemoveEndpointCommand(["my-endpoint"], new Config({} as any)).execute(
ctx.config
)
)
.it("clears the default if needed", (ctx) => {
expect(ctx.stdout).to.equal(`Removed endpoint my-endpoint.\n`);
expect(ctx.config.rootConfig).to.deep.equal({
defaultEndpoint: undefined,
endpoints: {
"other-endpoint": {
url: "http://bar.baz",
secret: "fn3333",
// These graphql bits are only saved if they differ from the
// default.
graphqlHost: "graphql.fauna.com",
graphqlPort: 443,
},
},
});
expect(ctx.config.saveRootConfig.calledOnce).to.be.true;
});
});
Loading