Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Added option to keep output. (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunikkk authored Oct 9, 2017
1 parent 53236f0 commit 4fea273
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions os/src/main/kotlin/com/gojuno/commander/os/Processes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import java.util.concurrent.TimeoutException

val home: String by lazy { System.getenv("HOME") }

fun log(message: String): Unit = println("[${Date()}]: $message")
fun log(message: String) = println("[${Date()}]: $message")

sealed class Notification {
data class Start(val process: Process, val output: File) : Notification()
Expand All @@ -24,6 +24,7 @@ fun process(
commandAndArgs: List<String>,
timeout: Pair<Int, TimeUnit>? = 30 to SECONDS,
redirectOutputTo: File? = null,
keepOutputOnExit: Boolean = false,
unbufferedOutput: Boolean = false,
print: Boolean = false,
destroyOnUnsubscribe: Boolean = false
Expand All @@ -33,8 +34,10 @@ fun process(
log("\nRun: $commandAndArgs")
}

val outputFile = when (redirectOutputTo) {
null -> prepareOutputFile(commandAndArgs)
val outputFile = when {
redirectOutputTo == null || redirectOutputTo.isDirectory -> {
prepareOutputFile(redirectOutputTo, keepOutputOnExit)
}
else -> redirectOutputTo
}

Expand Down Expand Up @@ -102,13 +105,15 @@ fun process(
.subscribeOn(io()) // Prevent subscriber thread from unnecessary blocking.
.observeOn(io()) // Allow to wait for process exit code.

private fun prepareOutputFile(commandAndArgs: List<String>): File = Random()
private fun prepareOutputFile(parent: File?, keepOnExit: Boolean): File = Random()
.nextInt()
.let { System.nanoTime() + it }
.let { name ->
File("$name.output").apply {
File(parent, "$name.output").apply {
createNewFile()
deleteOnExit()
if (!keepOnExit) {
deleteOnExit()
}
}
}

Expand Down

0 comments on commit 4fea273

Please sign in to comment.