Skip to content

Instantly share code, notes, and snippets.

@fiadliel
Created December 7, 2016 00:44
Show Gist options
  • Save fiadliel/f25bd082908c8785fefe953efae6a672 to your computer and use it in GitHub Desktop.
Save fiadliel/f25bd082908c8785fefe953efae6a672 to your computer and use it in GitHub Desktop.
import fs2._
case class ProcessState(exitValue: Task[Int],
output: Stream[Task, Byte],
input: Sink[Task, Byte],
error: Stream[Task, Byte])
object RunProgram {
val waitThreadStrategy = Strategy.fromCachedDaemonPool("wait-threads")
def runProgram(workingDirectory: Path, args: String*)(implicit S: Strategy): ProcessState = {
val pb = new ProcessBuilder(args: _*).directory(workingDirectory.toFile)
val process = pb.start()
val inStream =
io.readInputStreamAsync(Task.delay(process.getInputStream), 1024)
val outStream = io.writeOutputStream(Task.delay(process.getOutputStream))
val errStream =
io.readInputStreamAsync(Task.delay(process.getErrorStream), 1024)
val result = Task(process.waitFor)(waitThreadStrategy)
ProcessState(result, inStream, outStream, errStream)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment