/
index.js
executable file
·75 lines (65 loc) · 2.06 KB
/
index.js
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;
const program = require('commander');
function volumes(vol, memo) {
memo.push(vol);
return memo;
}
program
.version(require('./package.json').version)
.usage('[options] "command arg1 arg2..."')
.option('-i, --image <image>', 'Will run the command in this image', 'alpine')
.option('-e, --entrypoint <executable>', 'Will be used to override the image entrypoint')
.option('-v, --volume <dir>', 'Volumes (directories and/or files) to mount inside the container. Can be specified multiple times. Current directory will always be mounted', volumes, ['.'])
.option('-u, --user <uid[:guid]>', 'Will run the command as this user[:group]', '`id -u`:`id -g`')
.option('-o, --opts <options>', 'Additional docker run options', '')
.option('--debug', 'Run in debug mode')
.arguments('<cmd>')
.action(function (command, options) {
const cmd = 'docker';
var args = ['run', '--rm'];
if(options.entrypoint) {
args = args.concat(['--entrypoint', options.entrypoint])
};
options.volume.map(vol => {
const v = path.resolve(vol);
args.push('-v');
args.push(`${v}:${v}`);
});
args = args.concat([
'-w', process.cwd(),
'-u', options.user,
options.opts,
options.image
]);
args = args.concat(command);
const opts = {
shell: true
};
if(options.debug) {
console.log('INDOCKER DEBUG:');
console.log(' OPTIONS:\n', args);
console.log();
}
const ind = spawn(cmd, args, opts)
ind.stdout.on('data', (data) => {
console.log(`${data}`);
});
ind.stderr.on('data', (data) => {
console.error(`${data}`);
});
ind.on('close', (code) => {
if(code) {
console.error(`Child process exited with code ${code}`);
process.exit(code);
}
});
ind.on('error', (err) => {
console.error('Failed to start child process.');
process.exit(1);
});
});
program.parse(process.argv);