-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand.class.js
215 lines (186 loc) · 6.06 KB
/
command.class.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Begin ---------------------------------------------------------------
/**
* Mock-up of the Command class from VMware Aria Automation.
* Builds a process object with the command to execute.
*
* @author Stefan Schnell <[email protected]>
* @license MIT
* @version 0.3.0
*
* Hint: This mock-up works only with the Mozilla Rhino JavaScript
* engine.
*
* Checked with ...
* - Windows 10, Windows 11 and RHEL 9.2.
* - Rhino 1.7R4, 1.7.14 and 1.7.15.
* - Bellsoft JDK 11.0.23, Bellsoft JDK 17.0.11, Oracle OpenJDK 20.0.2,
* Bellsoft JDK 21.0.1 and Bellsoft JDK 22.0.1.
*/
/**
* Method | Type |Documented|Implemented|Mock-Up
* -------------------------------+-- ------+----------+-----------+-------
* Command | | X | X |
* execute | function| X | X |
* executeAndLog | function| X | X |
* input |attribute| X | X |
* output |attribute| X | X |
* result |attribute| X | X |
*/
/**
* Executes a command in the host operating system.
* Commands are dependent on the host operating system default shell.<br>
* Hint: Command can be created using string or array. If string value
* is provided command arguments are extracted by splitting the string
* using whitespace as separator.
*
* @param {string|string[]} command - The command to execute.
* @param {string} input - Input for the process of the command to execute.
* @returns {string} output - Standard output from the command.
* @returns {number} result - Return code from the command.
*
* @example
* var command = new Command("cmd /c dir");
* // var command = new Command("ls -l");
* command.execute(true);
* System.log("Return Code: " + String(command.result));
* System.log("Output: " + command.output);
*
* @example
* var command = new Command(["cmd", "/c", "dir", "/b"]);
* // var command = new Command(["ls", "-l", "-s"]);
* command.execute(true);
* System.log("Return Code: " + String(command.result));
* System.log("Output: " + command.output);
*
* @example
* var command = new Command("cmd");
* command.input = "dir /d\n"; // \n for enter.
* // var command = new Command("bash");
* // command.input = "ls -l -s\n";
* command.execute(true);
* System.log("Return Code: " + String(command.result));
* System.log("Output: " + command.output);
*/
var Command = function(command) {
if (
typeof command === "undefined" ||
command === null ||
arguments.length === 0
) {
throw new Error("command argument can not be undefined or null");
}
if (Array.isArray(command)) {
this._command = command;
} else {
if (arguments.length === 1) {
this._command = command.split(" ");
} else {
this._command = new Array(arguments.length);
for (var key = 0; key < arguments.length; key++) {
this._command[key] = arguments[key];
}
}
}
this.input = "";
this.output = "";
this.result = null;
};
Command.prototype = {
/**
* Executes the command.
*
* @function execute
* @param {boolean} wait - Wait for execution end.
* @returns {number}
*
* @example
* var command = new Command("notepad.exe");
* // var command = new Command("gedit");
* command.execute();
* System.log("Return Code: " + String(command.result));
*/
execute : function(wait) {
try {
var process = java.lang.Runtime.getRuntime().exec(this._command);
// Sends data to the process.
if (this.input != null) {
var input = new java.lang.String(String(this.input));
process.getOutputStream().write(input.getBytes());
process.getOutputStream().flush();
}
process.getOutputStream().close();
if (wait === true) {
// Get data of stdout from the process.
var output = new java.lang.String(
process.getInputStream().readAllBytes(),
java.nio.charset.StandardCharsets.ISO_8859_1
);
process.getInputStream().close();
// Get data of stderr from the process.
output += new java.lang.String(
process.getErrorStream().readAllBytes(),
java.nio.charset.StandardCharsets.ISO_8859_1
);
process.getErrorStream().close();
this.output = String(output);
process.waitFor();
this.result = process.exitValue();
} else {
process.getInputStream().close();
process.getErrorStream().close();
this.result = 0;
}
return this.result;
} catch (exception) {
throw new Error("\nError at Command.execute\n" +
exception.message + "\n");
}
},
/**
* Executes the command and logs the standard output to a file.
*
* @function executeAndLog
* @param {string} filename - The file where the output is redirected.
* @returns {number}
*
* @example
* var command = new Command("cmd", "/c", "dir", "/b");
* command.executeAndLog("C:\\Users\\Public\\dirTest.txt");
* // var command = new Command("ls", "-l" , "-s");
* // command.executeAndLog("/home/stefan/dirTest.txt");
*/
executeAndLog : function(filename) {
if (
typeof filename === "undefined" ||
filename === null ||
String(filename).trim() === ""
) {
throw new Error("filename argument can not be undefined or null");
}
this.execute(true);
var file;
try {
file = new java.io.FileWriter(filename);
file.write(this.output);
} catch (exception) {
throw new Error("\nError at Command.executeAndLog\n" +
exception.message + "\n");
} finally {
if (typeof file !== "undefined") {
file.close();
}
}
return this.result;
},
/**
* Returns the class name.<br>
* Hint: This method is a standard.
*
* @function getClassName
* @returns {string}
*/
getClassName: function() {
return "Command";
}
};
// End -----------------------------------------------------------------