package com.cadrlife.devsearch.agent.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; public class ExecService { private static final Logger LOG = LoggerFactory.getLogger(ExecService.class); public void gitClone(File pwd, String url) { execute(pwd, "git", "clone", url); } public void gitPull(File pwd) { execute(pwd, "git", "pull"); } public void execute(File pwd, String...cmd) { String[] envp = {}; exec(cmd, envp, pwd, logInfoAppendable(), logErrorAppendable()); } private Appendable logErrorAppendable() { return new LoggingWriter(new LoggingWriter.LogHook() { @Override public void log(String msg) { LOG.error(msg); } }); } private Appendable logInfoAppendable() { return new LoggingWriter(new LoggingWriter.LogHook() { @Override public void log(String msg) { LOG.info(msg); } }); } /** * Executes a command and waits for the process to exit. * The process is queried every 100 milliseconds for completion. * stdoutWriter and stderrWriter can be the same object. * * @param cmd the command to be passed to {@link Runtime#exec(String[])}. * @param stdoutWriter buffer to which the process's stdout will be written. can be null. * @param stderrWriter buffer to which the process's stderr will be written. can be null. * @return the exit value of the process * @throws RuntimeException IOException and InterruptedException are wrapped in RuntimeException and thrown */ private int exec(String[] cmd, String[] envp, File pwd, Appendable stdoutWriter, Appendable stderrWriter) { try { Process p = Runtime.getRuntime().exec(cmd, envp, pwd); InputStreamReader stdout = new InputStreamReader(p.getInputStream()); InputStreamReader stderr = new InputStreamReader(p.getErrorStream()); while (true) { int exitValue = -1; boolean done; try { exitValue = p.exitValue(); done = true; } catch (IllegalThreadStateException e) { done = false; } while (stdout.ready()) stdoutWriter.append((char)stdout.read()); while (stderr.ready()) stderrWriter.append((char)stderr.read()); if (done) return exitValue; Thread.sleep(100); } } catch (IOException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } } }