diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/index.js | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..d5d9668 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,44 @@ +const { promisify } = require('util'); +const { exec } = require('child_process'); + +const internals = { + kActiveIndicator: 'active', + kFieldSeparator: '\n', + kKeyValueSeparator: '=', + kBlockSeparator: '\n\n', + statusCommand: 'systemctl show --no-page -p Id -p ActiveState', + + formatStatusOutput(systemctlText) { + + const blocks = systemctlText.trim().split(internals.kBlockSeparator); + return blocks + .map((block) => { + + const fields = block.split(internals.kFieldSeparator); + return fields.reduce((fieldObject, field) => { + + const [key, value] = field.split(internals.kKeyValueSeparator); + return { + ...fieldObject, + [key]: value + } + }, {}) + + }) + .reduce((statusObject, service) => { + + return { + ...statusObject, + [service.Id]: service.ActiveState === internals.kActiveIndicator + } + }, {}); + }, + + exec: promisify(exec) +}; + +module.exports = async (services) => { + + const { stdout } = await internals.exec(`${internals.statusCommand} ${services.join(' ')}`); + return internals.formatStatusOutput(stdout); +}; |