]>
Commit | Line | Data |
---|---|---|
2d72aab0 RBR |
1 | const { promisify } = require('util'); |
2 | const { exec } = require('child_process'); | |
3 | ||
4 | const internals = { | |
5 | kActiveIndicator: 'active', | |
6 | kFieldSeparator: '\n', | |
7 | kKeyValueSeparator: '=', | |
8 | kBlockSeparator: '\n\n', | |
de8c8f36 | 9 | statusCommand: 'systemctl show --no-page -p Description -p ActiveState', |
2d72aab0 RBR |
10 | |
11 | formatStatusOutput(systemctlText) { | |
12 | ||
13 | const blocks = systemctlText.trim().split(internals.kBlockSeparator); | |
14 | return blocks | |
15 | .map((block) => { | |
16 | ||
17 | const fields = block.split(internals.kFieldSeparator); | |
18 | return fields.reduce((fieldObject, field) => { | |
19 | ||
20 | const [key, value] = field.split(internals.kKeyValueSeparator); | |
21 | return { | |
22 | ...fieldObject, | |
23 | [key]: value | |
24 | } | |
25 | }, {}) | |
26 | ||
27 | }) | |
28 | .reduce((statusObject, service) => { | |
29 | ||
30 | return { | |
31 | ...statusObject, | |
de8c8f36 | 32 | [service.Description]: service.ActiveState === internals.kActiveIndicator |
2d72aab0 RBR |
33 | } |
34 | }, {}); | |
35 | }, | |
36 | ||
37 | exec: promisify(exec) | |
38 | }; | |
39 | ||
40 | module.exports = async (services) => { | |
41 | ||
42 | const { stdout } = await internals.exec(`${internals.statusCommand} ${services.join(' ')}`); | |
43 | return internals.formatStatusOutput(stdout); | |
44 | }; |