forked from fauna/fauna-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list-keys.js
168 lines (158 loc) · 3.87 KB
/
list-keys.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const FaunaCommand = require('../lib/fauna-command.js')
const { errorOut } = require('../lib/misc.js')
const faunadb = require('faunadb')
const q = faunadb.query
const Table = require('cli-table')
/**
* See the cli-table docs: https://github.com/Automattic/cli-table
*/
function getTable() {
return new Table({
chars: {
top: '',
'top-mid': '',
'top-left': '',
'top-right': '',
bottom: '',
'bottom-mid': '',
'bottom-left': '',
'bottom-right': '',
left: '',
'left-mid': '',
mid: '',
'mid-mid': '',
right: '',
'right-mid': '',
middle: ' ',
},
head: ['Key ID', 'Database', 'Role'],
colWidths: [20, 20, 20],
style: { 'padding-left': 0, 'padding-right': 0 },
})
}
/**
* Sorts keys by database name.
*
* @param {Key} a - Key reference.
* @param {Key} b - Key reference.
*/
function compareByDBName(a, b) {
if (a.name < b.name) {
return -1
} else if (a.name > b.name) {
return 1
}
return 0
}
function buildTable(res) {
const table = getTable()
res.data.sort(compareByDBName)
res.data.forEach(function (el) {
const dbName = el.name
if (el.keys.data.length > 0) {
el.keys.data.forEach(function (key) {
table.push([key.id, dbName, key.role])
})
} else {
table.push(['No keys created', dbName, '-'])
}
})
return table
}
/**
* Returns the first 100 keys defined on the current Database.
* "100 keys ought to be enough for anybody".
*/
function currentDbKeysQuery(q) {
return q.Let(
{},
{
name: '[current]',
keys: q.Map(
q.Paginate(q.Keys(), { size: 100 }),
q.Lambda(
'key',
q.Let(
{
keyDoc: q.Get(q.Var('key')),
},
{
id: q.Select(['ref', 'id'], q.Var('keyDoc')),
role: q.Select(['role'], q.Var('keyDoc')),
}
)
)
),
}
)
}
/**
* Returns the first 100 keys defined per child Database within the current one.
* "100 keys ought to be enough for anybody".
* In a similar fashion a limit is set up for the first 100 children of the current Database.
*/
function childrenDbKeysQuery(q) {
return q.Map(
q.Paginate(q.Databases(), { size: 100 }),
q.Lambda(
'db',
q.Let(
{
dbDoc: q.Get(q.Var('db')),
},
{
name: q.Select(['name'], q.Var('dbDoc')),
keys: q.Map(
q.Paginate(q.Keys(q.Database(q.Select(['name'], q.Var('dbDoc')))), {
size: 100,
}),
q.Lambda(
'key',
q.Let(
{
keyDoc: q.Get(q.Var('key')),
},
{
id: q.Select(['ref', 'id'], q.Var('keyDoc')),
role: q.Select(['role'], q.Var('keyDoc')),
}
)
)
),
}
)
)
)
}
class ListKeysCommand extends FaunaCommand {
async run() {
const log = this.log
return this.withClient(async function (client, _) {
try {
// retrieving current and children db keys
const [currentDb, childrenDbs] = await Promise.all([
client.query(currentDbKeysQuery(q)),
client.query(childrenDbKeysQuery(q)),
])
// appending current db's keys to children,
// i.e. union all the keys together
childrenDbs.data.push(currentDb)
if (childrenDbs.data.length > 0) {
log(buildTable(childrenDbs).toString())
} else {
log('No databases found')
}
} catch (err) {
errorOut(err.message)
}
})
}
}
ListKeysCommand.description = `
List keys in the current database or in its child databases
`
ListKeysCommand.examples = ['$ fauna list-keys']
ListKeysCommand.flags = {
...FaunaCommand.flags,
}
module.exports = ListKeysCommand