Last active
November 16, 2018 16:24
-
-
Save gaerfield/c2c440a0853a18646e8a1720601fd102 to your computer and use it in GitHub Desktop.
Why kscript is awesome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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