Skip to content
Snippets Groups Projects
Commit aaf1d04d authored by Georg Seibt's avatar Georg Seibt :nerd:
Browse files

build generic ProcessExecutor class

parent 931aeb92
No related branches found
No related tags found
No related merge requests found
package de.uni_passau.fim.seibt.gitwrapper.process;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
public class ProcessExecutor {
private static final Logger LOG = Logger.getLogger(ProcessExecutor.class.getCanonicalName());
/**
* The result of a command line execution.
*/
......@@ -9,4 +19,54 @@ public class ProcessExecutor {
public int exitCode;
public String output;
}
public static Optional<ExecRes> exec(String cmd, File workingDir, boolean redirectError, String... parameters) {
if (parameters == null) {
return exec(workingDir, redirectError, cmd);
}
String[] cmdArray = new String[parameters.length + 1];
cmdArray[0] = cmd;
System.arraycopy(parameters, 0, cmdArray, 1, parameters.length);
return exec(workingDir, redirectError, cmdArray);
}
public static Optional<ExecRes> exec(File workingDir, boolean redirectError, String... command) {
if (command == null) {
return Optional.empty();
}
ProcessBuilder builder = new ProcessBuilder();
builder.command(command);
builder.directory(workingDir);
builder.redirectErrorStream(redirectError);
return exec(builder);
}
public static Optional<ExecRes> exec(ProcessBuilder builder) {
String cmd = String.join(" ", builder.command());
LOG.fine(() -> String.format("Executing '%s'.", cmd));
try {
ExecRes res = new ExecRes();
Process p = builder.start();
res.output = IOUtils.toString(p.getInputStream());
res.exitCode = p.waitFor();
LOG.fine(() -> String.format("Execution of '%s' returned exit code %d.", cmd, res.exitCode));
LOG.finest(() -> String.format("Execution of '%s' output:%n%s", cmd, res.output));
return Optional.of(res);
} catch (IOException | InterruptedException e) {
LOG.log(Level.SEVERE, e, () -> String.format("Exception executing '%s'.", cmd));
return Optional.empty();
}
}
}
package de.uni_passau.fim.seibt.gitwrapper.repo;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
import java.util.Scanner;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor;
import de.uni_passau.fim.seibt.gitwrapper.process.ProcessExecutor.ExecRes;
import org.apache.commons.io.IOUtils;
/**
* A wrapper for executing git commands on the command line.
......@@ -126,34 +124,6 @@ public class GitWrapper {
* @return the result of the execution
*/
public Optional<ExecRes> exec(File workingDirectory, String... parameters) {
ProcessBuilder b = new ProcessBuilder();
String[] cmdArray = new String[parameters.length + 1];
cmdArray[0] = git;
System.arraycopy(parameters, 0, cmdArray, 1, parameters.length);
b.command(cmdArray);
b.directory(workingDirectory);
b.redirectErrorStream(true);
String cmd = String.join(" ", b.command());
LOG.fine(() -> String.format("Executing '%s'.", cmd));
try {
ExecRes res = new ExecRes();
Process p = b.start();
res.output = IOUtils.toString(p.getInputStream());
res.exitCode = p.waitFor();
LOG.fine(() -> String.format("Execution of '%s' returned exit code %d.", cmd, res.exitCode));
LOG.finest(() -> String.format("Execution of '%s' output:%n%s", cmd, res.output));
return Optional.of(res);
} catch (IOException | InterruptedException e) {
LOG.log(Level.SEVERE, e, () -> String.format("Exception executing '%s'.", cmd));
return Optional.empty();
}
return ProcessExecutor.exec(git, workingDirectory, true, parameters);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment