20
src/commonMain/kotlin/com/infendro/shell/Shell.kt
Normal file
20
src/commonMain/kotlin/com/infendro/shell/Shell.kt
Normal file
@@ -0,0 +1,20 @@
|
||||
package com.infendro.shell
|
||||
|
||||
object Shell {
|
||||
suspend fun run(
|
||||
command: Array<String>,
|
||||
directory: String? = null,
|
||||
): Result {
|
||||
return execute(command, directory)
|
||||
}
|
||||
|
||||
class Result(
|
||||
val code: Int,
|
||||
val value: String,
|
||||
)
|
||||
}
|
||||
|
||||
internal expect suspend fun execute(
|
||||
command: Array<String>,
|
||||
directory: String?,
|
||||
): Shell.Result
|
||||
19
src/jsMain/kotlin/com/infendro/shell/Shell.js.kt
Normal file
19
src/jsMain/kotlin/com/infendro/shell/Shell.js.kt
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.infendro.shell
|
||||
|
||||
import kotlin.coroutines.resume
|
||||
import kotlin.coroutines.suspendCoroutine
|
||||
import node.childProcess.ExecOptions
|
||||
import node.childProcess.exec
|
||||
|
||||
internal actual suspend fun execute(
|
||||
command: Array<String>,
|
||||
directory: String?,
|
||||
): Shell.Result = suspendCoroutine { continuation ->
|
||||
val command = command.joinToString(" ") { "\"$it\"" }
|
||||
val options = ExecOptions.invoke(cwd = directory)
|
||||
exec(command, options) { error, stdout, stderr ->
|
||||
val code = if (error == null) 0 else error.code!!.toInt()
|
||||
val value = if (error == null) stdout else stderr
|
||||
continuation.resume(Shell.Result(code, value))
|
||||
}
|
||||
}
|
||||
21
src/jvmMain/kotlin/com/infendro/shell/Shell.jvm.kt
Normal file
21
src/jvmMain/kotlin/com/infendro/shell/Shell.jvm.kt
Normal file
@@ -0,0 +1,21 @@
|
||||
package com.infendro.shell
|
||||
|
||||
import java.io.File
|
||||
|
||||
internal actual suspend fun execute(
|
||||
command: Array<String>,
|
||||
directory: String?,
|
||||
): Shell.Result {
|
||||
val process = ProcessBuilder(*command)
|
||||
.also {
|
||||
if (directory != null) {
|
||||
it.directory(File(directory))
|
||||
}
|
||||
}
|
||||
.start()
|
||||
val code = process.waitFor()
|
||||
val value = process
|
||||
.let { if (code == 0) it.inputReader() else it.errorReader() }
|
||||
.readText()
|
||||
return Shell.Result(code, value)
|
||||
}
|
||||
34
src/nativeMain/kotlin/com/infendro/shell/Shell.native.kt
Normal file
34
src/nativeMain/kotlin/com/infendro/shell/Shell.native.kt
Normal file
@@ -0,0 +1,34 @@
|
||||
package com.infendro.shell
|
||||
|
||||
import kotlinx.cinterop.CPointer
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import kotlinx.cinterop.refTo
|
||||
import kotlinx.cinterop.toKString
|
||||
import platform.posix.*
|
||||
|
||||
@ExperimentalForeignApi
|
||||
internal actual suspend fun execute(
|
||||
command: Array<String>,
|
||||
directory: String?,
|
||||
): Shell.Result {
|
||||
val command = command.joinToString(" ") { "\"$it\"" }
|
||||
if (directory != null) {
|
||||
chdir(directory)
|
||||
}
|
||||
val stream = popen(command, "r")!!
|
||||
val value = stream.readLines().joinToString("")
|
||||
val code = pclose(stream)
|
||||
return Shell.Result(code, value)
|
||||
}
|
||||
|
||||
@ExperimentalForeignApi
|
||||
private fun CPointer<FILE>.readLines(): List<String> {
|
||||
val lines = mutableListOf<String>()
|
||||
val buffer = ByteArray(4096)
|
||||
while (true) {
|
||||
val line = fgets(buffer.refTo(0), buffer.size, this)
|
||||
if (line == null) break
|
||||
lines.add(line.toKString())
|
||||
}
|
||||
return lines
|
||||
}
|
||||
Reference in New Issue
Block a user