forked from fauna/fauna-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-key.js
62 lines (52 loc) · 1.46 KB
/
create-key.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
const FaunaCommand = require('../lib/fauna-command.js')
const { errorOut } = require('../lib/misc.js')
const faunadb = require('faunadb')
const q = faunadb.query
function successMessage(database, role, secret) {
return `
created key for database '${database}' with role '${role}'.
secret: ${secret}
To access '${database}' with this key, create a client using
the driver library for your language of choice using
the above secret.
`
}
class CreateKeyCommand extends FaunaCommand {
async run() {
const log = this.log
const dbname = this.args.dbname
const role = this.args.role || 'admin'
const { client } = await (dbname
? this.ensureDbScopeClient(dbname)
: this.getClient())
log(`creating key for database '${dbname}' with role '${role}'`)
return client
.query(q.CreateKey({ role }))
.then((success) => {
log(successMessage(dbname, success.role, success.secret))
})
.catch((error) => {
errorOut(error.message, 1)
})
}
}
CreateKeyCommand.description = `
Creates a key for the specified database
`
CreateKeyCommand.examples = ['$ fauna create-key dbname admin']
CreateKeyCommand.flags = {
...FaunaCommand.flags,
}
CreateKeyCommand.args = [
{
name: 'dbname',
required: true,
description: 'database name',
},
{
name: 'role',
description: 'key user role',
options: ['admin', 'server', 'server-readonly', 'client'],
},
]
module.exports = CreateKeyCommand