Co-routine, DSL-friendly process launching from Kotlin.
- Executing a process and capturing the results to a
List<String>
val result = execToList { commandLine("git", "version") }
result.output.forEach { println(it) }
- Executing a process and processing standard output as a
Sequence<String>
val result = exec {
commandLine("git", "version")
consumeLineSequence { it.sumOf { it.length } }
}
- Executing a process and writing standard output to a file
execToFile(File("git.version")) { commandLine("git", "version") }
- Executing a process and process the standard output stream
val result = exec {
commandLine("git", "version")
consumeStream { inputStream ->
val file = File.createTempFile("git.", ".version")
GZIPOutputStream(file.outputStream()).use { inputStream.copyTo(it) }
file
}
}
For executing multiple commands in a single shell session, use the shellSession
function:
shellSession {
mkdir("foo")
cd("foo")
exec("git", "init")
}
- Non-zero exit values are considered a failure by default (configure
failOnNonZeroExit
) and throw an exception; - Standard error is, by default, captured to a
List<String>
Gradle:
dependencies {
implementation("io.cloudshiftdev.kprocess:kprocess:<version>")
}