How to invoke external command from within Kotlin code?

Example of running a git diff by shelling out: “git diff”.runCommand(gitRepoDir) Here are two implementations of the runCommand extension function: 1. Redirect to stdout/stderr This wires any output from the subprocess to regular stdout and stderr: fun String.runCommand(workingDir: File) { ProcessBuilder(*split(” “).toTypedArray()) .directory(workingDir) .redirectOutput(Redirect.INHERIT) .redirectError(Redirect.INHERIT) .start() .waitFor(60, TimeUnit.MINUTES) } 2. Capturing output as a String … Read more