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);
}
}
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);
}
}
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);
}
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);
}
}
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)");
}
Aggregations