Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1090: added list handling to ProcessContext #1349

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ default ProcessContext directory(Path directory) {
*/
int run(String... command);

/**
* @param commands the list of commands to run.
* @return the exit code. Will be {@link #SUCCESS} on successful completion of the {@link Process}.
*/
int run(List<String> commands);

/**
* @param command the command to run.
* @return a {@link List} with the standard output of the command, line by line.
*/
List<String> runAndGetStdOut(String... command);

/**
* @param commands the list of commands to run.
* @return a {@link List} with the standard output of the command, line by line.
*/
List<String> runAndGetStdOut(List<String> commands);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public int run(String... command) {
return runCommand(null, command);
}

@Override
public int run(List<String> commands) {

return runCommand(null, commands);
}

@Override
public List<String> runAndGetStdOut(String... command) {

Expand All @@ -64,16 +70,40 @@ public List<String> runAndGetStdOut(String... command) {
return out;
}

@Override
public List<String> runAndGetStdOut(List<String> command) {

List<String> out = new ArrayList<>();
runCommand(out, command);
return out;
}

private int runCommand(List<String> out, List<String> commands){
if ((commands == null) || (commands.isEmpty())) {
throw new IllegalArgumentException("Commands must not be empty!");
}

this.processBuilder.command(commands);
return processCommand(out, commands.toString());
}

private int runCommand(List<String> out, String... command) {

if ((command == null) || (command.length < 1) || command[0].isBlank()) {
throw new IllegalArgumentException("Command must not be empty!");
}

this.processBuilder.command(command);
return processCommand(out, command);
}

private int processCommand(List<String> out, String... command) {

if (this.logger.debug().isEnabled()) {
String message = createCommandMessage(" ...", command);
this.logger.debug(message);
}
this.processBuilder.command(command);

try {
if (out == null) {
this.processBuilder.redirectOutput(Redirect.INHERIT);
Expand Down