use of com.google.copybara.shell.BadExitStatusException 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);
}
}
}
use of com.google.copybara.shell.BadExitStatusException in project copybara by google.
the class GitCredential method fill.
/**
* Execute 'git credential fill' for a url
*
* @param cwd the directory to execute the command. This is important if credential configuration
* is set in the local git config.
* @param url url to get the credentials from
* @return a username and password
* @throws RepoException If the url doesn't have protocol (For example https://), the url is
* not valid or username/password couldn't be found
*/
public UserPassword fill(Path cwd, String url) throws RepoException, ValidationException {
Map<String, String> env = Maps.newHashMap(this.env);
// Prevent from asking user for the password (It goes directly to the terminal, not to the
// passed InputStream/OutputStream.
env.put("GIT_TERMINAL_PROMPT", "0");
URI uri;
try {
uri = URI.create(url);
} catch (IllegalArgumentException e) {
throw new ValidationException(e, "Cannot get credentials for " + url);
}
String protocol = uri.getScheme();
if (Strings.isNullOrEmpty(protocol)) {
throw new ValidationException("Cannot find the protocol for " + url);
}
String host = uri.getHost();
Command cmd = new Command(new String[] { gitBinary, "credential", "fill" }, env, cwd.toFile());
String request = String.format("protocol=%s\nhost=%s\n", protocol, host);
if (!Strings.isNullOrEmpty(uri.getPath())) {
request += String.format("path=%s\n", uri.getPath());
}
request += "\n";
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
try {
// DON'T REPLACE THIS WITH CommandRunner.execute(). WE DON'T WANT TO ACCIDENTALLY LOG THE
// PASSWORD!
CommandResult result = cmd.execute(new ByteArrayInputStream(request.getBytes(UTF_8)), new TimeoutKillableObserver(timeout.toMillis()), out, err);
if (!result.getTerminationStatus().success()) {
throw new RepoException("Error getting credentials:\n" + new String(err.toByteArray(), UTF_8));
}
} catch (BadExitStatusException e) {
String errStr = new String(err.toByteArray(), UTF_8);
if (errStr.contains("could not read")) {
throw new ValidationException("Interactive prompting of passwords for git is disabled," + " use git credential store before calling Copybara.");
}
throw new RepoException("Error getting credentials:\n" + errStr, e);
} catch (CommandException e) {
throw new RepoException("Error getting credentials", e);
}
Map<String, String> map = Splitter.on(NEW_LINE).omitEmptyStrings().withKeyValueSeparator("=").split(new String(out.toByteArray(), UTF_8));
if (!map.containsKey("username")) {
throw new RepoException("git credentials for " + url + " didn't return a username");
}
if (!map.containsKey("password")) {
throw new RepoException("git credentials for " + url + " didn't return a password");
}
return new UserPassword(map.get("username"), map.get("password"));
}
Aggregations