26 lines
735 B
Kotlin
26 lines
735 B
Kotlin
package static_analysis.utils
|
|
|
|
import java.io.File
|
|
import java.io.IOException
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
fun String.runCommand(
|
|
directoryToExecute: File? = null,
|
|
timeoutSec: Long = 30,
|
|
): String? {
|
|
return try {
|
|
val parts = this.split("\\s".toRegex())
|
|
val process = ProcessBuilder(*parts.toTypedArray())
|
|
.directory(directoryToExecute)
|
|
.redirectOutput(ProcessBuilder.Redirect.PIPE)
|
|
.redirectError(ProcessBuilder.Redirect.PIPE)
|
|
.start()
|
|
|
|
process.waitFor(timeoutSec, TimeUnit.SECONDS)
|
|
process.inputStream.bufferedReader().readText()
|
|
} catch(e: IOException) {
|
|
e.printStackTrace()
|
|
null
|
|
}
|
|
}
|