use of com.google.copybara.shell.TerminationStatus in project copybara by google.
the class CommandRunner method execute.
/**
* Executes a {@link Command} with the given input and writes to the console and the log depending
* on the exit code of the command and the verbose flag.
*/
public CommandOutputWithStatus execute() throws CommandException {
Stopwatch stopwatch = Stopwatch.createStarted();
String startMsg = "Executing [" + ShellUtils.prettyPrintArgv(Arrays.asList(cmd.getCommandLineElements())) + "]";
logger.log(Level.INFO, startMsg);
if (verbose) {
System.err.println(startMsg);
}
ByteArrayOutputStream stdoutCollector = new ByteArrayOutputStream();
ByteArrayOutputStream stderrCollector = new ByteArrayOutputStream();
OutputStream stdoutStream = commandOutputStream(stdoutCollector);
OutputStream stderrStream = commandOutputStream(stderrCollector);
TerminationStatus exitStatus = null;
try {
CommandResult cmdResult = cmd.execute(input, new TimeoutKillableObserver(COMMAND_TIMEOUT), stdoutStream, stderrStream, true);
exitStatus = cmdResult.getTerminationStatus();
return new CommandOutputWithStatus(cmdResult.getTerminationStatus(), stdoutCollector.toByteArray(), stderrCollector.toByteArray());
} catch (BadExitStatusException e) {
exitStatus = e.getResult().getTerminationStatus();
throw new BadExitStatusWithOutputException(e.getCommand(), e.getResult(), e.getMessage(), stdoutCollector.toByteArray(), stderrCollector.toByteArray());
} finally {
String commandName = cmd.getCommandLineElements()[0];
logOutput(Level.INFO, String.format("'%s' STDOUT: ", commandName), stdoutCollector, maxOutLogLines);
logOutput(Level.INFO, String.format("'%s' STDERR: ", commandName), stderrCollector, maxOutLogLines);
String finishMsg = String.format("Command '%s' finished in %s. %s", commandName, stopwatch, exitStatus != null ? exitStatus.toString() : "(No exit status)");
logger.log(Level.INFO, finishMsg);
if (verbose) {
System.err.println(finishMsg);
}
}
}
Aggregations