/
docker-cmd.js
138 lines (128 loc) · 3.82 KB
/
docker-cmd.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'use strict';
var child_process = require('child_process');
/**
* This is a DockerCmd representing "docker" command line
* @constructor
*/
function DockerCmd() {
}
/**
* @callback DockerCmd~callback
* @param {number} dockerProcessExitCode - the docker process exit code (0 if all was OK)
*/
/**
* Execute the given <code>commandName</code> with the given <code>dockerOptions</code> and <code>commandOptions</code>.
* @param {string} commandName
* @param {Options} commandOptions
* @param {Object} dockerOptions
* @param {DockerCmd~callback} callback
*/
DockerCmd.prototype.executeCommand = function(commandName, commandOptions, dockerOptions, callback) {
// put all options in an array to give to "spawn" later
var cmdOptions = ['docker'];
// first the docker options to pass before the docker command
appendOptions(cmdOptions, dockerOptions);
// then the docker command
cmdOptions.push(commandName);
// and finally the command options with potentially final args (using the '_' field)
appendOptions(cmdOptions, commandOptions);
var dockerProcess = child_process.spawn('/usr/bin/env', cmdOptions, {stdio: 'inherit'});
dockerProcess.on('close', callback);
};
/**
* @param {string} commandName
* @return {function(this:DockerCmd, Options, Object, DockerCmd~callback)}
* @private
*/
DockerCmd.prototype._createDefaultCommand = function(commandName) {
var self = this;
/**
* @param {Options} commandOptions
* @param {Object} dockerOptions
* @param {DockerCmd~callback} callback
*/
return function(commandOptions, dockerOptions, callback) {
self.executeCommand(commandName, commandOptions, dockerOptions, callback);
}
};
/// Declare all the docker commands
[
'attach',
'build',
'commit',
'cp',
'diff',
'events',
'export',
'history',
'images',
'import',
'info',
'inspect',
'kill',
'load',
'login',
'logout',
'logs',
'port',
'pause',
'ps',
'pull',
'push',
'restart',
'rm',
'run',
'save',
'search',
'start',
'stop',
'tag',
'top',
'unpause',
'version',
'wait'
].forEach(function (commandName) {
DockerCmd.prototype[commandName] = DockerCmd.prototype._createDefaultCommand(commandName);
});
/**
* Append each option from the given <code>fromOptions</code> to the given
* <code>options</code> array, flattening them to pass them later as parameters to a
* sub call process.
* @param {string[]} options
* @param {Options} fromOptions
*/
function appendOptions(options, fromOptions) {
function pushOption(optionName, optionValue) {
var valueDefined = optionValue !== null && optionValue !== undefined;
if (optionName.length === 1) {
// simple letter option
options.push('-' + optionName);
if (valueDefined) {
options.push(optionValue);
}
} else {
// full option name
options.push('--' + optionName + (valueDefined ? '=' + optionValue : ''));
}
}
for (var optionName in fromOptions) {
if (fromOptions.hasOwnProperty(optionName) && optionName !== '_') {
var optionValue = fromOptions[optionName];
if (Array.isArray(optionValue)) {
// we have multiple values for the same option, let's iterate on each
optionValue.forEach(function (iOptionValue) {
pushOption(optionName, iOptionValue);
});
} else {
pushOption(optionName, optionValue);
}
}
}
// now append the "_" which are not "options" but args
if (fromOptions && fromOptions._) {
[].concat(fromOptions._).forEach(function(arg) {
options.push(arg);
});
}
}
module.exports = DockerCmd;