【问题标题】:Kotlin-native execute command and get the outputKotlin-native 执行命令并获取输出
【发布时间】:2023-11-26 23:07:01
【问题描述】:

我想知道 kotlin native 中是否有办法通过 posix 调用命令并接收它的终端输出。例如,我想让“git diff”命令工作,而不必创建一个临时文件,将输出写入它然后从该文件中读取。

在 SO 上,我只找到了需要 ProcessBuilder 的解决方案,它在 kotlin-native 上不可用,因为它是一个 Java 库。

【问题讨论】:

    标签: terminal posix kotlin-native


    【解决方案1】:

    我找到了一段我想使用的工作代码,所以我将它发布在这里以供将来的查看者使用!

    fun executeCommand(command: String): String{
        val fp: CPointer<FILE>? = popen(command, "r")
        val buffer = ByteArray(4096)
        val returnString = StringBuilder()
    
        /* Open the command for reading. */
        if (fp == NULL) {
            printf("Failed to run command\n" )
            exit(1)
        }
    
        /* Read the output a line at a time - output it. */
        var scan = fgets(buffer.refTo(0), buffer.size, fp)
        if(scan != null) {
            while (scan != NULL) {
                returnString.append(scan!!.toKString())
                scan = fgets(buffer.refTo(0), buffer.size, fp)
            }
        }
        /* close */
        pclose(fp)
        return returnString.trim().toString()
    }
    

    【讨论】:

    【解决方案2】:

    它是 mg-lolenstine 发布的 Kotlin Native 的 exec 命令的改进版本,它使用命令 stderr 引发异常,而不是仅仅返回 exit(1)(这本身并不总是可取的行为),现在 trim 也是可选的

    import kotlinx.cinterop.*
    import platform.posix.*
    
    fun executeCommand(
        command: String,
        trim: Boolean = true,
        redirectStderr: Boolean = true
    ): String {
        val commandToExecute = if (redirectStderr) "$command 2>&1" else command
        val fp = popen(commandToExecute, "r") ?: error("Failed to run command: $command")
    
        val stdout = buildString {
            val buffer = ByteArray(4096)
            while (true) {
                val input = fgets(buffer.refTo(0), buffer.size, fp) ?: break
                append(input.toKString())
            }
        }
    
        val status = pclose(fp)
        if (status != 0) {
            error("Command `$command` failed with status $status${if (redirectStderr) ": $stdout" else ""}")
        }
    
        return if (trim) stdout.trim() else stdout
    }
    

    【讨论】:

    【解决方案3】:

    我使用上面的答案来创建一个运行任意本地进程/命令的工作 gradle kotlin native/jvm 多平台多项目:

    这是我的结果:

    https://github.com/hoffipublic/minimal_kotlin_multiplatform

    import kotlinx.cinterop.refTo
    import kotlinx.cinterop.toKString
    import platform.posix.fgets
    import platform.posix.pclose
    import platform.posix.popen
    
    actual object MppProcess : IMppProcess {
        actual override fun executeCommand(
            command: String,
            redirectStderr: Boolean
        ): String? {
            val commandToExecute = if (redirectStderr) "$command 2>&1" else command
            val fp = popen(commandToExecute, "r") ?: error("Failed to run command: $command")
    
            val stdout = buildString {
                val buffer = ByteArray(4096)
                while (true) {
                    val input = fgets(buffer.refTo(0), buffer.size, fp) ?: break
                    append(input.toKString())
                }
            }
    
            val status = pclose(fp)
            if (status != 0) {
                error("Command `$command` failed with status $status${if (redirectStderr) ": $stdout" else ""}")
            }
    
            return stdout
        }
    }
    

    在 jvm 上

    import java.util.concurrent.TimeUnit
    
    actual object MppProcess : IMppProcess {
        actual override fun executeCommand(
            command: String,
            redirectStderr: Boolean
        ): String? {
            return runCatching {
                ProcessBuilder(command.split(Regex("(?<!(\"|').{0,255}) | (?!.*\\1.*)")))
                    //.directory(workingDir)
                    .redirectOutput(ProcessBuilder.Redirect.PIPE)
                    .apply { if (redirectStderr) this.redirectError(ProcessBuilder.Redirect.PIPE) }
                    .start().apply { waitFor(60L, TimeUnit.SECONDS) }
                    .inputStream.bufferedReader().readText()
            }.onFailure { it.printStackTrace() }.getOrNull()
        }
    }
    

    【讨论】: