forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.ts
82 lines (64 loc) · 2.17 KB
/
logs.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { flags } from '@oclif/command'
import inquirer from 'inquirer'
import { Kommand } from '../../common'
import { execute } from '../../support/execute'
export class InstanceLogs extends Kommand {
static initSdk = false
static description = 'Displays the logs of a running Kuzzle'
static flags = {
instance: flags.string({
char: 'i',
description: 'Kuzzle instance name',
}),
follow: flags.boolean({
char: 'f',
description: 'Follow log output',
}),
}
async runSafe() {
let instance: string = this.flags.instance
const followOption: boolean = this.flags.follow
if (!instance) {
const instancesList = await this.getInstancesList()
if (instancesList.length === 0) {
throw new Error('There are no Kuzzle running instances')
}
const responses: any = await inquirer.prompt([{
name: 'instance',
message: 'On which kuzzle instance do you want to see the logs',
type: 'list',
choices: instancesList,
}])
if (!responses.instance) {
throw new Error('A running Kuzzle instance must be selected.');
}
instance = responses.instance
}
await this.showInstanceLogs(instance, followOption)
}
private async showInstanceLogs(instanceName: string, followOption: boolean) {
const args = ['logs', instanceName]
if (followOption) {
args.push('-f')
}
const instanceLogs = execute('docker', ...args)
instanceLogs.process.stdout?.pipe(process.stdout)
instanceLogs.process.stderr?.pipe(process.stderr)
await instanceLogs
}
private async getInstancesList(): Promise<string[]> {
let containersListProcess
try {
containersListProcess = await execute('docker', 'ps', '--format', '"{{.Names}}"')
}
catch {
this.warn('Something went wrong while getting kuzzle running instances list')
return []
}
const containersList: string[] = containersListProcess.stdout.replace(/"/g, '').split('\n')
return containersList.filter(containerName =>
(containerName.includes('kuzzle')
&& !containerName.includes('redis')
&& !containerName.includes('elasticsearch')))
}
}