Search in sources :

Example 1 with TerminationStatus

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);
        }
    }
}
Also used : TerminationStatus(com.google.copybara.shell.TerminationStatus) TimeoutKillableObserver(com.google.copybara.shell.TimeoutKillableObserver) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Stopwatch(com.google.common.base.Stopwatch) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadExitStatusException(com.google.copybara.shell.BadExitStatusException) CommandResult(com.google.copybara.shell.CommandResult)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)1 BadExitStatusException (com.google.copybara.shell.BadExitStatusException)1 CommandResult (com.google.copybara.shell.CommandResult)1 TerminationStatus (com.google.copybara.shell.TerminationStatus)1 TimeoutKillableObserver (com.google.copybara.shell.TimeoutKillableObserver)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1