datamaintain.core.util.ProcessUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datamaintain-core Show documentation
Show all versions of datamaintain-core Show documentation
One tool to maintain all your database schemas!
package datamaintain.core.util
import datamaintain.core.exception.DatamaintainProcessException
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.util.concurrent.TimeUnit
fun List.runProcess(
workingDir: File = File("."),
timeoutValue: Long = 60,
timeoutUnit: TimeUnit = TimeUnit.MINUTES,
outputReadFunction: ((InputStream) -> Unit)? = null
): Int {
try {
val proc = ProcessBuilder(*this.toTypedArray())
.directory(workingDir)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
if (outputReadFunction != null) {
outputReadFunction(proc.inputStream)
}
proc.waitFor(timeoutValue, timeoutUnit)
return proc.exitValue()
} catch (e: IOException) {
throw DatamaintainProcessException(this, e.message ?: "")
}
}