diff --git a/README.md b/README.md index 76d14df9..b703861b 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 ] [--timeout - ] [--secret ] [--endpoint ] - -ARGUMENTS - ENDPOINT_ALIAS Fauna server endpoint alias - -FLAGS - --endpoint= Connection endpoint, from ~/.fauna-shell - --endpointURL= Database URL. Overrides the `url` in ~/.fauna-shell - --secret= Secret key. Overrides the `secret` in ~/.fauna-shell - --timeout= 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. @@ -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. diff --git a/src/commands/default-endpoint.js b/src/commands/default-endpoint.js deleted file mode 100644 index 1ea25026..00000000 --- a/src/commands/default-endpoint.js +++ /dev/null @@ -1,30 +0,0 @@ -const { setDefaultEndpoint } = require("../lib/misc.js"); -const { Args } = require("@oclif/core"); -const FaunaCommand = require("../lib/fauna-command.js").default; - -class DefaultEndpointCommand extends FaunaCommand { - async run() { - return setDefaultEndpoint(this.args.endpoint_alias) - .then(this.log) - .catch((err) => { - this.error(err.message, 1); - }); - } -} - -DefaultEndpointCommand.description = "Set an endpoint as the default one."; - -DefaultEndpointCommand.examples = ["$ fauna default-endpoint endpoint"]; - -// clear the default FaunaCommand flags that accept --host, --port, etc. -DefaultEndpointCommand.flags = {}; - -DefaultEndpointCommand.args = { - // eslint-disable-next-line camelcase - endpoint_alias: Args.string({ - required: true, - description: "Fauna server endpoint alias", - }), -}; - -module.exports = DefaultEndpointCommand; diff --git a/src/commands/endpoint/select.ts b/src/commands/endpoint/select.ts new file mode 100644 index 00000000..e8ab367d --- /dev/null +++ b/src/commands/endpoint/select.ts @@ -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) => ({ + 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}.`); + } +}