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

Update default-endpoint to fauna endpoint select #288

Merged
merged 3 commits into from
Oct 10, 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
53 changes: 25 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,6 @@ the queries file on the default fauna shell endpoint.
- [`fauna cloud-login`](#fauna-cloud-login)
- [`fauna create-database`](#fauna-create-database)
- [`fauna create-key`](#fauna-create-key)
- [`fauna default-endpoint`](#fauna-default-endpoint)
- [`fauna delete-database`](#fauna-delete-database)
- [`fauna delete-key`](#fauna-delete-key)
- [`fauna endpoint`](#fauna-endpoint)
Expand Down Expand Up @@ -576,33 +575,6 @@ EXAMPLES
$ fauna create-key dbname admin
```

## `fauna default-endpoint`

Set an endpoint as the default one.

```sh
Set an endpoint as the default one.

USAGE
$ fauna default-endpoint ENDPOINT_ALIAS [--endpointURL <value>] [--timeout
<value>] [--secret <value>] [--endpoint <value>]

ARGUMENTS
ENDPOINT_ALIAS Fauna server endpoint alias

FLAGS
--endpoint=<value> Connection endpoint, from ~/.fauna-shell
--endpointURL=<value> Database URL. Overrides the `url` in ~/.fauna-shell
--secret=<value> Secret key. Overrides the `secret` in ~/.fauna-shell
--timeout=<value> Connection timeout in milliseconds

DESCRIPTION
Set an endpoint as the default one.

EXAMPLES
$ fauna default-endpoint endpoint
```

## `fauna delete-database`

Delete the given database. Warning: this action cannot be undone.
Expand Down Expand Up @@ -755,6 +727,31 @@ EXAMPLES
$ fauna endpoint remove my_endpoint
```

### `fauna endpoint select`

Set an endpoint as the default one.

```sh
Set an endpoint as the default one.

USAGE
$ fauna endpoint select [NAME]

ARGUMENTS
NAME New default endpoint

DESCRIPTION
Set an endpoint as the default one.

ALIASES
$ fauna default-endpoint

EXAMPLES
$ fauna endpoint select

$ fauna endpoint select endpoint
```

## `fauna eval`

Evaluate the given query.
Expand Down
30 changes: 0 additions & 30 deletions src/commands/default-endpoint.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/commands/endpoint/select.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ShellConfig } from "../../lib/config/index";
import { Args, Command } from "@oclif/core";
import { searchSelect } from "../../lib/search-select";

export default class DefaultEndpointCommand extends Command {
static description = "Set an endpoint as the default one.";

static examples = [
"$ fauna endpoint select",
"$ fauna endpoint select endpoint",
];

static args = {
name: Args.string({
required: false,
description: "New default endpoint",
}),
};

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

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

await this.execute(config);
}

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

if (Object.keys(config.rootConfig.endpoints).length === 0) {
this.error("No endpoints defined. Create one with `fauna cloud-login`");
}

const name =
args.name ??
(await searchSelect({
message: "Select a new default endpoint",
choices: Object.keys(config.rootConfig.endpoints).map((endpoint) => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if there are no endpoints here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't work. I'll add a check for no endpoints, and error early and say something like "create an endpoin with fauna cloud-login"

value: endpoint,
})),
}));

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

config.rootConfig.defaultEndpoint = name;
config.saveRootConfig();

this.log(`Updated default endpoint to ${name}.`);
}
}