package lc.mecha.exec;

import org.apache.commons.lang.ArrayUtils;

import java.util.List;

/**
 * Implementation of {@link ProcessExecutor} which executes a process on the local machine.
 *
 * @author Alex Leigh
 */
public class LocalExecutor implements ProcessExecutor {
    @Override
    public ProcessExecution execute(String cmd, List<String> args) throws Exception {
        return execute(cmd, args.stream().toArray(String[]::new));
    }

    @Override
    public ProcessExecution execute(String cmd, String[] args) throws Exception {
        Runtime rt = Runtime.getRuntime();
        Process proc = rt.exec((String[]) ArrayUtils.addAll(new String[]{cmd}, args));
        return new ProcessExecutionImpl(proc.getInputStream(), proc.getOutputStream(), proc.getErrorStream());
    }

    @Override
    public void close() throws Exception {
        // Intentionally Blank
    }
}
