Skip to content

Instantly share code, notes, and snippets.

@gaerfield
Last active November 16, 2018 16:24
Show Gist options
  • Save gaerfield/c2c440a0853a18646e8a1720601fd102 to your computer and use it in GitHub Desktop.
Save gaerfield/c2c440a0853a18646e8a1720601fd102 to your computer and use it in GitHub Desktop.
Why kscript is awesome
import java.io.File
/** Executes a shell-command and returns the result */
fun String.exe(dir: File? = null): String {
val process = ProcessBuilder("/bin/sh", "-c", this)
.redirectErrorStream(true)
.directory(dir)
.start()
val exitCode = process.waitFor()
if(exitCode != 0) {
println("Execution of command '$this' failed with ExitCode '$exitCode'")
System.exit(exitCode)
}
return process.inputStream.bufferedReader().use { it.readText() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment