package smalljava;

import java.io.IOException;

public class Exec {
    public static String exec(String... command)  throws IOException, InterruptedException {
        if (System.getProperty("smalljava.Exec.dryrun") != null) {
            IO.println(String.join(" ", command));
            return "";
        } 

        Process p = Runtime.getRuntime().exec(command);
        p.waitFor();
        var output = new String(p.getInputStream().readAllBytes());
        var error = new String(p.getErrorStream().readAllBytes());
        if (error.length() > 0) throw new RuntimeException(error);
        return output;
    }
}
