diff --git a/README.md b/README.md index 76d2a1b..5ecddf7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,9 @@ -# systemctl +# systemctl-cmd This simple node module allows you to control system services using the systemctl command easily and asynchronously. +This is a fork of VolantisDev's systemctl, because he isn't accepting pull requests. + ## Requirements - Archlinux or another OS which uses the systemctl command @@ -13,13 +15,16 @@ There are two ways to use systemctl depending on your needs: ### 1. Utilize the helper functions ``` // For ES6: -import systemctl from 'systemctl' +import systemctl from 'systemctl-cmd' // For ES5: -var systemctl = require('systemctl') +var systemctl = require('systemctl-cmd') // Start a service systemctl.start('service-name').then(output => console.log) +// Start a service with sudo +systemctl.start('service-name', true).then(output => console.log) + // Check if a service is enabled systemctl.isEnabled('service-name').then(enabled => { console.log((enabled ? 'Enabled' : 'Not enabled')); diff --git a/index.js b/index.js index 901470d..d632221 100644 --- a/index.js +++ b/index.js @@ -2,46 +2,71 @@ var Promise = require('bluebird') var run = require('./run') function daemonReload() { - return run("daemon-reload") + return run("daemon-reload"); } function disable(serviceName) { - return run("disable", serviceName) + return run("disable", serviceName); } function enable(serviceName) { - return run("enable", serviceName) + return run("enable", serviceName); } function isEnabled(serviceName) { return new Promise((resolve, reject) => { run('is-enabled', serviceName) .then((result) => { - resolve(result.stdout.indexOf('enabled') != -1) + resolve(result.stdout.indexOf('enabled') != -1); }) .catch(function (err) { - resolve(false) + resolve(false); }) }) } -function restart(serviceName) { - return run("restart", serviceName) +function isActive(serviceName) { + return new Promise((resolve, reject) => { + run('is-active', serviceName) + .then((result) => { + if(result.stdout.indexOf('active') != -1 && result.stdout.indexOf('inactive')==-1) + resolve(true); + else + resolve(false); + }) + .catch(function (err) { + resolve(false); + }) + }) +} + +function restart(serviceName, sudo=false) { + return run("restart", serviceName, sudo); +} + +function start(serviceName, sudo=false) { + return run("start", serviceName, sudo); +} + +function stop(serviceName, sudo=false) { + return run("stop", serviceName, sudo); } -function start(serviceName) { - return run("start", serviceName) +function mask(serviceName, sudo=false) { + return run("mask", serviceName, sudo); } -function stop(serviceName) { - return run("stop", serviceName) +function unmask(serviceName, sudo=false) { + return run("unmask", serviceName, sudo); } -module.exports.run = run -module.exports.daemonReload = daemonReload -module.exports.disable = disable -module.exports.enable = enable -module.exports.isEnabled = isEnabled -module.exports.restart = restart -module.exports.start = start -module.exports.stop = stop +module.exports.run = run; +module.exports.daemonReload = daemonReload; +module.exports.disable = disable; +module.exports.enable = enable; +module.exports.isEnabled = isEnabled; +module.exports.restart = restart; +module.exports.start = start; +module.exports.stop = stop; +module.exports.mask = mask; +module.exports.unmask = unmask; diff --git a/package-lock.json b/package-lock.json index c950b65..c8a09d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "systemctl", - "version": "0.2.0", + "version": "0.2.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 2239cf6..1cd9d8b 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,11 @@ { - "name": "systemctl", - "version": "0.2.2", + "name": "systemctl-cmd", + "version": "0.2.4", "description": "Allows you to control system services using the systemctl command easily and asynchronously", - "repository": "bmcclure/node-systemctl", + "repository": { + "type": "git", + "url": "git@github.com:justinschw/systemctl-cmd.git" + }, "keywords": [ "Systemctl", "Systemd", @@ -11,17 +14,23 @@ ], "author": "Ben McClure (volantisdev.com)", "license": "MIT", - "homepage": "https://github.com/bmcclure/node-systemctl", "bugs": { "url": "https://github.com/bmcclure/node-systemctl/issues" }, "contributors": [ "Ben McClure (volantisdev.com)", - "Frank Lemanschik" + "Frank Lemanschik", + "Justin Schwartzbeck ", + "chaojinn" ], "dependencies": { "bluebird": "^3.5.0", "child-process-promise": "^2.2.1" + }, + "main": "index.js", + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" } } diff --git a/run.js b/run.js index 0827445..0d528de 100644 --- a/run.js +++ b/run.js @@ -1,11 +1,7 @@ var exec = require("child-process-promise").exec -module.exports = function run(cmd, service_name) { - var command = 'systemctl ' + cmd +module.exports = function run(cmd, service_name, sudo = false) { + var command = `${(sudo) ? 'sudo ' : ''}systemctl ${cmd} ${service_name}`; - if (service_name) { - command = command + ' ' + service_name - } - - return exec(command) + return exec(command); }