forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.ts
63 lines (47 loc) · 1.37 KB
/
export.ts
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
import fs from 'fs'
import path from 'path'
import { flags } from '@oclif/command'
import { Kommand } from '../../common'
import { kuzzleFlags, } from '../../support/kuzzle'
export default class ProfileExport extends Kommand {
static description = 'Exports profiles'
static flags = {
help: flags.help({}),
path: flags.string({
description: 'Dump directory',
default: 'profiles'
}),
...kuzzleFlags,
protocol: flags.string({
description: 'Kuzzle protocol (http or websocket)',
default: 'ws',
}),
}
async runSafe() {
const filename = path.join(this.flags.path, 'profiles.json')
this.logInfo(`Exporting profiles in ${filename} ...`)
fs.mkdirSync(this.flags.path, { recursive: true })
const profiles = await this._dumpProfiles()
const dump = {
type: 'profiles',
content: profiles
}
fs.writeFileSync(filename, JSON.stringify(dump, null, 2))
this.logOk(`${Object.keys(profiles).length} profiles dumped`)
}
async _dumpProfiles() {
const options = {
scroll: '3s',
size: 100
}
let result = await this.sdk.security.searchProfiles({}, options)
const profiles: any = {}
while (result) {
result.hits.forEach((hit: any) => {
profiles[hit._id] = { policies: hit.policies }
})
result = await result.next()
}
return profiles
}
}