add-to-systemd
Advanced tools
Comparing version 1.0.1 to 1.1.0
47
index.js
@@ -6,7 +6,36 @@ #!/usr/bin/env node | ||
var cp = require('child_process') | ||
var minimist = require('minimist') | ||
var i = 2 | ||
var name = null | ||
var skip = false | ||
for (; i < process.argv.length; i++) { | ||
if (process.argv[i] === '--') continue | ||
if (skip) { | ||
skip = false | ||
continue | ||
} | ||
if (process.argv[i][0] === '-') { | ||
skip = process.argv[i].indexOf('=') === -1 | ||
continue | ||
} | ||
if (name) break | ||
else name = process.argv[i] | ||
} | ||
var argv = minimist(process.argv.slice(0, i), { | ||
alias: {user: 'u', cwd: 'c', env: 'e'} | ||
}) | ||
var TEMPLATE = fs.readFileSync(path.join(__dirname, 'template.service'), 'utf-8') | ||
if (!process.argv[3]) { | ||
console.error('Usage: add-to-systemd name command...') | ||
if (!name) { | ||
console.error('Usage: add-to-systemd name [options] command...') | ||
console.error() | ||
console.error(' --user, -u [user] User the service will run as') | ||
console.error(' --cwd, -c [dir] Set the cwd of the service') | ||
console.error(' --env, -e [name=val] Add env vars to the service') | ||
console.error() | ||
process.exit(1) | ||
@@ -19,7 +48,15 @@ } | ||
var name = process.argv[2] | ||
var command = process.argv.slice(3).join(' ') | ||
var service = TEMPLATE.replace('{command}', command) | ||
var opts = '' | ||
if (argv.user) opts += 'User=' + argv.user + '\n' | ||
if (argv.cwd) opts += 'WorkingDirectory=' + fs.realpathSync(argv.cwd) + '\n' | ||
if (argv.env) { | ||
[].concat(argv.env).forEach(function (e) { | ||
opts += 'Environment=' + e + '\n' | ||
}) | ||
} | ||
var command = process.argv.slice(i).join(' ') | ||
var service = TEMPLATE.replace('{command}', command).replace('{options}', opts) | ||
fs.writeFileSync('/lib/systemd/system/' + name + '.service', service) | ||
cp.spawn('systemctl', ['enable', name + '.service'], {stdio: 'inherit'}) |
{ | ||
"name": "add-to-systemd", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Small command line tool to simply add a service to systemd", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"minimist": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
@@ -8,0 +10,0 @@ "standard": "^8.6.0" |
Sorry, the diff of this file is not supported yet
3912
50
1
+ Addedminimist@^1.2.0
+ Addedminimist@1.2.8(transitive)