se.svt.oss.mediaanalyzer.util.ProcessUtil.kt Maven / Gradle / Ivy
// SPDX-FileCopyrightText: 2020 Sveriges Television AB
//
// SPDX-License-Identifier: Apache-2.0
package se.svt.oss.mediaanalyzer.util
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import mu.KotlinLogging
internal object ProcessUtil {
private val log = KotlinLogging.logger { }
internal inline fun runAndParse(objectMapper: ObjectMapper, vararg command: String): Pair {
log.debug { "Running '${command.joinToString(" ")}'" }
val process = ProcessBuilder(command.toList()).start()
val output = process.inputStream.bufferedReader().use { it.readText() }
val exitCode = process.waitFor()
log.trace { "${command.firstOrNull()} finished with exit code: $exitCode" }
val parsed = try {
objectMapper.readValue(output)
} catch (e: Exception) {
throw RuntimeException("Error parsing ${T::class.simpleName} from output: '$output'", e)
}
return Pair(exitCode, parsed)
}
}