Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support kotlin continuations #57

Open
yschimke opened this issue Mar 24, 2018 · 1 comment
Open

Support kotlin continuations #57

yschimke opened this issue Mar 24, 2018 · 1 comment

Comments

@yschimke
Copy link

e.g. feel free to adapt if useful, I'm using this in my own project.

import okio.ByteString
import org.apache.commons.io.input.NullInputStream
import org.zeroturnaround.exec.ProcessExecutor
import org.zeroturnaround.exec.ProcessResult
import org.zeroturnaround.exec.listener.ProcessListener
import org.zeroturnaround.exec.stream.slf4j.Slf4jStream
import java.nio.charset.StandardCharsets
import kotlin.coroutines.experimental.suspendCoroutine

data class ExecResult(val exitCode: Int, val output: ByteString?) {
  val success = exitCode == 0
  val outputString by lazy {
    output?.string(StandardCharsets.UTF_8)!!
  }
}

val stdErrLogging = Slf4jStream.ofCaller().asInfo()

suspend fun exec(vararg command: String): ExecResult {
  return exec(command.toList()) {
    readOutput(true)
    redirectError(stdErrLogging)
    redirectInput(NullInputStream(0))
  }
}

suspend fun exec(command: List<String>, configure: ProcessExecutor.() -> Unit = {}): ExecResult {
  var outputRequested = false

  val pe = object : ProcessExecutor() {
    override fun readOutput(readOutput: Boolean): ProcessExecutor {
      outputRequested = readOutput
      return super.readOutput(readOutput)
    }
  }

  pe.command(command.toList())

  configure(pe)

  return suspendCoroutine { cont ->
    pe.addListener(object : ProcessListener() {
      override fun afterFinish(process: Process, result: ProcessResult) {
        try {
          val outputString = if (outputRequested) {
            val output = result.output()
            ByteString.of(output, 0, output.size)
          } else {
            null
          }

          cont.resume(ExecResult(result.exitValue, outputString))
        } catch (e: Exception) {
          cont.resumeWithException(e)
        }
      }
    })

    try {
      pe.start()
    } catch (e: Exception) {
      cont.resumeWithException(e)
    }
  }
}
@yschimke
Copy link
Author

    val result = exec(command) {
      timeout(5, TimeUnit.SECONDS)
      redirectError(stdErrLogging)
      readOutput(true)
    }

    println(result.output)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant