Files
shell.kt/src/macosMain/kotlin/com/infendro/shell/Shell.macos.kt
2026-02-16 17:53:50 +01:00

21 lines
622 B
Kotlin

package com.infendro.shell
import platform.posix.chdir
import platform.posix.pclose
import platform.posix.popen
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
val command = command.joinToString(" ") { """"$it"""" }
if (directory != null) {
chdir(directory)
}
val stream = popen(command, "r")!!
val value = stream.readLines().joinToString("")
val code = pclose(stream)
val result = Result.from(code, value)
continuation.resume(result)
}