forked from mafintosh/docker-run
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.js
executable file
·103 lines (87 loc) · 2.21 KB
/
bin.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env node
var run = require('./')
var minimist = require('minimist')
var fs = require('fs')
var path = require('path')
var argv = minimist(process.argv.slice(2), {
boolean: ['tty', 'remove', 'version', 'privileged'],
alias: {
tty:'t',
remove:'r',
host:'h',
net:'n',
port:'p',
env:'e',
volume:'v',
dns:'d'
},
'--': true,
default: {
tty: process.stdin.isTTY && process.stdout.isTTY,
remove: true,
width: process.stdout.columns,
height: process.stdout.rows
}
})
if (argv.version) {
console.log(require('./package').version)
return process.exit()
}
if (!argv._.length || argv.help) {
console.error(fs.readFileSync(require.resolve('./help.txt'), 'utf-8'))
return process.exit(argv.help ? 0 : 1)
}
var parsePorts = function() {
if (!argv.port) return null
return [].concat(argv.port).reduce(function(ports, p) {
var parts = p.toString().split(':')
ports[parts[0]] = parts[1] || parts[0]
return ports
}, {})
}
var parseEnv = function() {
if (!argv.env) return null
return [].concat(argv.env).reduce(function(env, e) {
e = e.toString()
var i = e.indexOf('=')
if (i === -1) return env
env[e.slice(0, i)] = e.slice(i+1).replace(/^["']|["']$/, '')
return env
}, {})
}
var parseVolumes = function() {
if (!argv.volume) return null
return [].concat(argv.volume).reduce(function(volumes, v) {
var parts = v.split(':')
volumes[path.resolve(process.cwd(), parts[0])] = parts[1] || parts[0]
return volumes
}, {})
}
argv.argv = argv['--']
argv.ports = parsePorts()
argv.env = parseEnv()
argv.volumes = parseVolumes()
var image = argv._[0]
var child = run(image, argv)
if (argv.tty && !argv.fork) process.stdin.setRawMode(true)
if (!argv.fork) {
process.stdin.pipe(child.stdin)
child.stdout.pipe(process.stdout)
child.stderr.pipe(process.stderr)
}
process.stdout.on('resize', function() {
child.resize(process.stdout.columns, process.stdout.rows)
})
child.on('error', function(err) {
console.error('Error: %s', err.message)
process.exit(1)
})
child.on('exit', function(code) {
process.exit(code)
})
process.on('SIGINT', function() {
child.destroy()
})
process.on('SIGTERM', function() {
child.destroy()
})