You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB
Kotlin

package me.eater.hefbrug.access
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import org.apache.logging.log4j.kotlin.Logging
import java.io.File
import java.nio.charset.Charset
class Local : AccessSkeleton, Logging {
override fun id() = "local"
override suspend fun execute(
vararg command: String,
environment: Map<String, String>,
workingDirectory: String?
): ExecutionOutput {
val process = GlobalScope.async(Dispatchers.Unconfined) {
ProcessBuilder()
.apply {
command(*command)
if (workingDirectory != null)
directory(File(workingDirectory))
environment().putAll(environment)
}
.start()
.apply {
waitFor()
}
}.await()
return ExecutionOutput(
ExecutionCommand(command, environment, workingDirectory),
process.exitValue(),
process.inputStream.bufferedReader(Charset.forName("UTF-8")).readText(),
process.errorStream.bufferedReader(Charset.forName("UTF-8")).readText()
)
}
}