Search in sources :

Example 16 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class BuildifierFormat method run.

/**
 * Runs buildifier with the given arguments.
 */
private void run(Console console, Path checkoutDir, Iterable<String> args) throws IOException, ValidationException {
    ImmutableList.Builder<String> argBuilder = new Builder<String>().add(buildifierOptions.buildifierBin);
    if (type != null) {
        argBuilder.add("-type=" + type);
    }
    if (lintMode != LintMode.OFF) {
        argBuilder.add("-lint=" + lintMode.toString().toLowerCase());
        if (!warnings.isEmpty()) {
            argBuilder.add("-warnings=" + Joiner.on(",").join(warnings));
        }
    }
    String[] argv = argBuilder.addAll(args).build().toArray(new String[0]);
    try {
        Command cmd = new Command(argv, /*environmentVariables*/
        null, checkoutDir.toFile());
        CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(generalOptions.isVerbose()).execute();
        if (!output.getStdout().isEmpty()) {
            logger.atInfo().log("buildifier stdout: %s", output.getStdout());
        }
        if (!output.getStderr().isEmpty()) {
            logger.atInfo().log("buildifier stderr: %s", output.getStderr());
        }
    } catch (BadExitStatusWithOutputException e) {
        log(console, e.getOutput());
        checkCondition(e.getResult().getTerminationStatus().getExitCode() != 1, "Build file(s) couldn't be formatted because there was a syntax error");
        throw new IOException("Failed to execute buildifier with args: " + args, e);
    } catch (CommandException e) {
        throw new IOException("Failed to execute buildifier with args: " + args, e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 17 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class DiffUtil method checkNotInsideGitRepo.

/**
 * It is very common for users to have a git repo for their $HOME, so that they can version their
 * configurations. Unfortunately this fails for the default output directory (inside $HOME).
 */
public static void checkNotInsideGitRepo(Path path, boolean verbose, GitEnvironment gitEnv) throws IOException, InsideGitDirException {
    try {
        Command cmd = new Command(new String[] { gitEnv.resolveGitBinary(), "rev-parse", "--git-dir" }, gitEnv.getEnvironment(), path.toFile());
        String gitDir = new CommandRunner(cmd).withVerbose(verbose).execute().getStdout().trim();
        // If it doesn't fail it means taht we are inside a git directory
        throw new InsideGitDirException(String.format("Cannot diff/patch because Copybara temporary directory (%s) is inside a git" + " directory (%s).", path, gitDir), gitDir, path);
    } catch (BadExitStatusWithOutputException e) {
        // Some git versions return "Not", others "not"
        if (!e.getOutput().getStderr().contains(/*N*/
        "ot a git repository")) {
            throw new IOException("Error executing rev-parse", e);
        }
    } catch (CommandException e) {
        throw new IOException("Error executing rev-parse", e);
    }
}
Also used : Command(com.google.copybara.shell.Command) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 18 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class PatchingOptions method getPatchVersion.

Version getPatchVersion(String patchBin) throws CommandException, ValidationException {
    String out = new CommandRunner(new Command(new String[] { patchBin, "-v" })).withVerbose(generalOptions.isVerbose()).execute().getStdout().trim();
    Matcher matcher = PATCH_VERSION_FORMAT.matcher(out);
    checkCondition(matcher.matches(), "Unknown version of GNU Patch. Path used: %s. Use %s to use a different path", this.patchBin, PATCH_BIN_FLAG);
    int major = Integer.parseInt(matcher.group("major"));
    int minor = Integer.parseInt(matcher.group("minor"));
    return new Version(major, minor);
}
Also used : Command(com.google.copybara.shell.Command) Matcher(java.util.regex.Matcher) CommandRunner(com.google.copybara.util.CommandRunner)

Example 19 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class QuiltTransformation method runQuiltCommand.

private void runQuiltCommand(Path checkoutDir, Map<String, String> env, boolean verbose, String... args) throws IOException {
    ImmutableList.Builder<String> params = ImmutableList.builder();
    params.add(options.quiltBin);
    params.add(args);
    ImmutableList<String> paramsList = params.build();
    Command cmd = new Command(paramsList.toArray(new String[0]), env, checkoutDir.toFile());
    try {
        options.getGeneralOptions().newCommandRunner(cmd).withVerbose(verbose).execute();
    } catch (BadExitStatusWithOutputException e) {
        throw new IOException(String.format("Error executing '%s': %s. Stderr: \n%s", String.join(" ", paramsList), e.getMessage(), e.getOutput().getStdout()), e);
    } catch (CommandException e) {
        throw new IOException(e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 20 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class CommandRunnerTest method testCommandWithMaxLogLines.

@Test
public void testCommandWithMaxLogLines() throws Exception {
    Command command = new Command(new String[] { "echo", "hello\n", "world" });
    CommandOutputWithStatus result = runCommand(new CommandRunner(command).withMaxStdOutLogLines(1));
    assertThat(result.getTerminationStatus().success()).isTrue();
    assertThat(result.getStdout()).isEqualTo("hello\n world\n");
    assertThat(outContent.toByteArray()).isEmpty();
    assertLogContains("Executing [echo 'hello\n' world]", "'echo' STDOUT: hello", "'echo' STDOUT: ... truncated after 1 line(s)");
}
Also used : Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Aggregations

Command (com.google.copybara.shell.Command)28 CommandRunner (com.google.copybara.util.CommandRunner)17 CommandException (com.google.copybara.shell.CommandException)14 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)7 ImmutableList (com.google.common.collect.ImmutableList)6 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 RepoException (com.google.copybara.exception.RepoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ValidationException (com.google.copybara.exception.ValidationException)2 KillableObserver (com.google.copybara.shell.KillableObserver)2 CommandTimeoutException (com.google.copybara.util.CommandTimeoutException)2 Matcher (java.util.regex.Matcher)2 GitEnvironment (com.google.copybara.git.GitEnvironment)1 AbnormalTerminationException (com.google.copybara.shell.AbnormalTerminationException)1 BadExitStatusException (com.google.copybara.shell.BadExitStatusException)1 CommandResult (com.google.copybara.shell.CommandResult)1