forked from fauna/fauna-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-endpoint.js
72 lines (63 loc) · 1.86 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const { cli } = require('cli-ux')
const { flags } = require('@oclif/command')
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
let secret = this.flags.key
let alias = this.flags.alias
const log = this.log
const newEndpoint = url.parse(endpoint)
if (!newEndpoint.hostname) {
throw new Error('You must provide a valid endpoint.')
}
if (!secret)
secret = await cli.prompt('Endpoint Key', {
type: 'hide',
timeout: 120000,
})
if (!alias)
alias = await cli.prompt('Endpoint Alias', {
default: newEndpoint.hostname,
timeout: 120000,
})
if (!this.flags.alias && (alias === 'default' || alias === 'cloud')) {
throw new `The word '${alias}' cannot be used 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',
'$ fauna add-endpoint http://localhost:8443/ --alias localhost --key secret',
]
// clear the default FaunaCommand flags that accept --host, --port, etc.
AddEndpointCommand.flags = {
alias: flags.string({
description: 'FaunaDB server endpoint alias',
required: false,
}),
key: flags.string({
description: 'FaunaDB server endpoint key',
required: false,
}),
}
AddEndpointCommand.args = [
{
name: 'endpoint',
required: true,
description: 'FaunaDB server endpoint',
},
]
module.exports = AddEndpointCommand