forked from guacsec/trustify-da-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperations.java
More file actions
370 lines (336 loc) · 13.7 KB
/
Operations.java
File metadata and controls
370 lines (336 loc) · 13.7 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
* Copyright © 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.redhat.exhort.tools;
import static java.lang.String.join;
import com.redhat.exhort.logging.LoggersFactory;
import com.redhat.exhort.utils.Environment;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/** Utility class used for executing process on the operating system. * */
public final class Operations {
// Some package providers might return Unix output in Windows, so we need to use a generic line
// separator
public static final String GENERIC_LINE_SEPARATOR = "\\r?\\n";
private static final Logger log = LoggersFactory.getLogger(Operations.class.getName());
private Operations() {
// constructor not required for a utility class
}
/**
* Function for looking up custom executable path based on the default one provides as an
* argument. I.e. if defaultExecutable=mvn, this function will look for a custom mvn path set as
* an environment variable or a java property with the name EXHORT_MVN_PATH. If not found, the
* original mvn passed as defaultExecutable will be returned. Note, environment variables takes
* precedence on java properties.
*
* @param defaultExecutable default executable (uppercase spaces and dashes will be replaced with
* underscores).
* @return the custom path from the relevant environment variable or the original argument.
*/
public static String getCustomPathOrElse(String defaultExecutable) {
var target = defaultExecutable.toUpperCase().replaceAll(" ", "_").replaceAll("-", "_");
var executableKey = String.format("EXHORT_%s_PATH", target);
return Environment.get(executableKey, defaultExecutable);
}
/**
* Function for building a command from the command parts list and execute it as a process on the
* operating system. Will throw a RuntimeException if the command build or execution failed.
*
* @param cmdList list of command parts
*/
public static void runProcess(final String... cmdList) {
runProcess(null, cmdList, null);
}
public static void runProcess(final Path workingDirectory, final String... cmdList) {
runProcess(workingDirectory, cmdList, null);
}
public static void runProcess(final String[] cmdList, final Map<String, String> envMap) {
runProcess(null, cmdList, null);
}
public static void runProcess(
final Path workingDirectory, final String[] cmdList, final Map<String, String> envMap) {
var processBuilder = new ProcessBuilder();
processBuilder.command(cmdList);
if (workingDirectory != null) {
processBuilder.directory(workingDirectory.toFile());
}
if (envMap != null) {
processBuilder.environment().putAll(envMap);
}
// create a process builder or throw a runtime exception
Process process;
try {
process = processBuilder.start();
} catch (final IOException e) {
throw new RuntimeException(
String.format(
"failed to build process for '%s' got %s", join(" ", cmdList), e.getMessage()));
}
// execute the command or throw runtime exception if failed
int exitCode;
try {
exitCode = process.waitFor();
} catch (final InterruptedException e) {
throw new RuntimeException(
String.format(
"built process for '%s' interrupted, got %s", join(" ", cmdList), e.getMessage()));
}
// verify the command was executed successfully or throw a runtime exception
if (exitCode != 0) {
String errMsg;
try (var reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
errMsg = reader.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
throw new RuntimeException(
String.format(
"unable to process error output for '%s', got %s",
join(" ", cmdList), e.getMessage()));
}
if (errMsg.isEmpty()) {
try (var reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
errMsg = reader.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
throw new RuntimeException(
String.format(
"unable to process output for '%s', got %s", join(" ", cmdList), e.getMessage()));
}
}
if (errMsg.isEmpty()) {
throw new RuntimeException(
String.format("failed to execute '%s', exit-code %d", join(" ", cmdList), exitCode));
} else {
throw new RuntimeException(
String.format(
"failed to execute '%s', exit-code %d, message:%s%s%s",
join(" ", cmdList),
exitCode,
System.lineSeparator(),
errMsg,
System.lineSeparator()));
}
}
}
public static String runProcessGetOutput(Path dir, final String... cmdList) {
return runProcessGetOutput(dir, cmdList, null);
}
public static String runProcessGetOutput(Path dir, final String[] cmdList, String[] envList) {
try {
Process process;
if (dir == null) {
if (envList != null) {
process = Runtime.getRuntime().exec(cmdList, envList);
} else {
process = Runtime.getRuntime().exec(cmdList, null);
}
} else {
if (envList != null) {
process = Runtime.getRuntime().exec(cmdList, envList, dir.toFile());
} else {
process = Runtime.getRuntime().exec(cmdList, null, dir.toFile());
}
}
String stdout = new String(process.getInputStream().readAllBytes());
if (!stdout.isBlank()) {
return stdout.trim();
}
// TODO: This should throw an exception if the process fails
String stderr = new String(process.getErrorStream().readAllBytes());
return stderr.trim();
} catch (IOException e) {
throw new RuntimeException(
String.format("Failed to execute command '%s' ", join(" ", cmdList)), e);
}
}
public static ProcessExecOutput runProcessGetFullOutput(
Path dir, final String[] cmdList, String[] envList) {
try {
Process process;
if (dir == null) {
if (envList != null) {
process = Runtime.getRuntime().exec(cmdList, envList);
} else {
process = Runtime.getRuntime().exec(cmdList);
}
} else {
if (envList != null) {
process = Runtime.getRuntime().exec(cmdList, envList, dir.toFile());
} else {
process = Runtime.getRuntime().exec(cmdList, null, dir.toFile());
}
}
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line);
if (!line.endsWith(System.lineSeparator())) {
output.append("\n");
}
}
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
StringBuilder error = new StringBuilder();
while ((line = reader.readLine()) != null) {
error.append(line);
if (!line.endsWith(System.lineSeparator())) {
error.append("\n");
}
}
process.waitFor(30L, TimeUnit.SECONDS);
return new ProcessExecOutput(output.toString(), error.toString(), process.exitValue());
} catch (IOException | InterruptedException e) {
throw new RuntimeException(
String.format("Failed to execute command '%s' ", join(" ", cmdList)), e);
}
}
public static class ProcessExecOutput {
private final String output;
private final String error;
private final int exitCode;
public ProcessExecOutput(String output, String error, int exitCode) {
this.output = output;
this.error = error;
this.exitCode = exitCode;
}
public String getOutput() {
return output;
}
public String getError() {
return error;
}
public int getExitCode() {
return exitCode;
}
}
/**
* Retrieves the executable path for a given command, verifying that it exists and can be executed
* successfully.
*
* <p>This method uses {@link Operations#getCustomPathOrElse(String)} to obtain the path to the
* executable for the specified command. It then attempts to run the executable with the
* "--version" argument to verify that it is functional. If the executable is not found, cannot be
* executed, or exits with a non-zero status code, a {@link RuntimeException} is thrown.
*
* @param command the name of the command (e.g., "mvn", "npm", "yarn") for which to find the
* executable
* @return the path to the executable for the specified command
* @throws RuntimeException if the executable cannot be found, is not executable, or exits with an
* error code
*/
public static String getExecutable(
String command, String args, final Map<String, String> envMap) {
String cmdExecutable = Operations.getCustomPathOrElse(command);
try {
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command(cmdExecutable, args);
if (envMap != null) {
processBuilder.environment().putAll(envMap);
}
Process process = processBuilder.start();
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException(
command + " executable found, but it exited with error code " + exitCode);
}
} catch (IOException | InterruptedException e) {
throw new RuntimeException(
String.format(
"Unable to find or run "
+ command
+ " executable '%s'. Please ensure "
+ command
+ " is installed and"
+ " available in your PATH.",
cmdExecutable),
e);
}
return cmdExecutable;
}
public static String getExecutable(String command, String args) {
return getExecutable(command, args, null);
}
/**
* Checks whether a wrapper preference is set for a given tool name.
*
* <p>This method looks for an environment variable with the name {@code
* EXHORT_PREFER_<TOOLNAME>W}, where {@code <TOOLNAME>} is the uppercase form of the provided
* {@code name} parameter. If the environment variable is present (i.e., not {@code null}), the
* method returns {@code true}.
*
* @param name the tool name for which to check the wrapper preference (e.g., "maven", "gradle")
* @return {@code true} if the corresponding environment variable is set; {@code false} otherwise
*/
public static boolean getWrapperPreference(String name) {
return Environment.get("EXHORT_PREFER_" + name.toUpperCase() + "W") != null;
}
/**
* Retrieves a Maven configuration value from an environment variable.
*
* @param configName the configuration name (e.g., "USER_SETTINGS", "LOCAL_REPO")
* @return the configuration value if set and not blank, null otherwise
*/
public static String getMavenConfig(String configName) {
String configValue = Environment.get("EXHORT_MVN_" + configName);
return (configValue != null && !configValue.isBlank()) ? configValue : null;
}
/**
* Attempts to retrieve the root directory of a Git repository for the given working directory.
*
* <p>This method runs the command {@code git rev-parse --show-toplevel} in the specified
* directory, which returns the absolute path to the root of the current Git working tree. If the
* command executes successfully and produces output, the trimmed result is returned as an {@link
* Optional}. If the command fails or the output is empty, an empty {@code Optional} is returned.
*
* @param cwd the working directory in which to execute the Git command
* @return an {@code Optional} containing the Git root directory path if found, otherwise {@code
* Optional.empty()}
*/
public static Optional<String> getGitRootDir(String cwd) {
List<String> command = Arrays.asList("git", "rev-parse", "--show-toplevel");
ProcessBuilder builder =
new ProcessBuilder(command).directory(new File(cwd)).redirectErrorStream(true);
try {
Process process = builder.start();
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = reader.readLine();
int exitCode = process.waitFor();
if (exitCode == 0) {
return Optional.ofNullable(line).map(String::trim);
} else {
log.warning("Git command exited with code " + exitCode + " in directory " + cwd);
}
}
} catch (IOException e) {
log.warning("I/O error executing git in " + cwd + ": " + e.getMessage());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.warning("Git command interrupted in directory " + cwd + ": " + e.getMessage());
}
return Optional.empty();
}
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
}
}