1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Better callback support.

This commit is contained in:
Cedric Beust 2015-11-13 17:29:04 -08:00
parent cdeea8862d
commit 6dac9a92c1

View file

@ -7,8 +7,9 @@ import java.io.InputStreamReader
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
open class RunCommand(val command: String) { open class RunCommand(val command: String) {
val defaultSuccess = { output: List<String> -> } val DEFAULT_SUCCESS = { output: List<String> -> }
// val defaultSuccessVerbose = { output: List<String> -> log(2, "Success:\n " + output.joinToString("\n"))} // val DEFAULT_SUCCESS_VERBOSE = { output: List<String> -> log(2, "Success:\n " + output.joinToString("\n"))}
val defaultSuccess = DEFAULT_SUCCESS
val defaultError = { val defaultError = {
output: List<String> -> error("Error:\n " + output.joinToString("\n")) output: List<String> -> error("Error:\n " + output.joinToString("\n"))
} }
@ -16,8 +17,9 @@ open class RunCommand(val command: String) {
var directory = File(".") var directory = File(".")
var env = hashMapOf<String, String>() var env = hashMapOf<String, String>()
fun run(args: List<String>, errorCb: Function1<List<String>, Unit> = defaultError, fun run(args: List<String>,
successCb: Function1<List<String>, Unit> = defaultSuccess) : Int { errorCallback: Function1<List<String>, Unit> = defaultError,
successCallback: Function1<List<String>, Unit> = defaultSuccess) : Int {
val allArgs = arrayListOf<String>() val allArgs = arrayListOf<String>()
allArgs.add(command) allArgs.add(command)
allArgs.addAll(args) allArgs.addAll(args)
@ -34,9 +36,9 @@ open class RunCommand(val command: String) {
val callSucceeded = process.waitFor(30, TimeUnit.SECONDS) val callSucceeded = process.waitFor(30, TimeUnit.SECONDS)
// val callSucceeded = if (passed == 0) true else false // val callSucceeded = if (passed == 0) true else false
if (callSucceeded) { if (callSucceeded) {
successCb(fromStream(process.inputStream)) successCallback(fromStream(process.inputStream))
} else { } else {
errorCb(listOf("$command failed") + fromStream(process.errorStream)) errorCallback(listOf("$command failed") + fromStream(process.errorStream))
} }
return if (callSucceeded) 0 else 1 return if (callSucceeded) 0 else 1