/
add-endpoint.js
55 lines (47 loc) · 1.46 KB
/
add-endpoint.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const {cli} = require('cli-ux')
const {saveEndpointOrError, errorOut} = require('../lib/misc.js')
const FaunaCommand = require('../lib/fauna-command.js')
const url = require('url')
class AddEndpointCommand extends FaunaCommand {
async run() {
const endpoint = this.args.endpoint
const log = this.log
const newEndpoint = url.parse(endpoint)
if (!newEndpoint.hostname) {
throw new Error('You must provide a valid endpoint.')
}
return cli.prompt('Endpoint Key', {type: 'hide', timeout: 120000})
.then(function (secret) {
return cli.prompt('Endpoint Alias', {default: newEndpoint.hostname, timeout: 120000})
.then(function (alias) {
if (alias === 'default' || alias === 'cloud') {
throw new Error(`The word '${alias}' cannot be usded as an alias.`)
}
return saveEndpointOrError(newEndpoint, alias, secret)
.then(function (_) {
log(`Endpoint '${alias}' saved.`)
})
})
})
.catch(function (err) {
errorOut(err.message, 1)
})
}
}
AddEndpointCommand.description = `
Adds a connection endpoint for FaunaDB
`
AddEndpointCommand.examples = [
'$ fauna add-endpoint https://db.fauna.com:443',
]
// clear the default FaunaCommand flags that accept --host, --port, etc.
AddEndpointCommand.flags = {
}
AddEndpointCommand.args = [
{
name: 'endpoint',
required: true,
description: 'FaunaDB server endpoint',
},
]
module.exports = AddEndpointCommand