All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cc.unitmesh.prompt.validate.ExtToolValidator.kt Maven / Gradle / Ivy

package cc.unitmesh.prompt.validate

/**
 * Extension Tool is validate command, like `plantuml xxx.puml`
 */
class ExtToolValidator(private val execCommand: String, override val input: String, val options: Map) :
    Validator {
    override fun validate(): Boolean {
        // a exec command like `ls -l`
        val commandList = execCommand.split(" ").toMutableList()

        for ((key, value) in options) {
            commandList.add(key)
            commandList.add(value)
        }

        if (input.isNotEmpty()) {
            commandList.add(input)
        }

        val processBuilder = ProcessBuilder(commandList)

        return try {
            val process = processBuilder.start()
            val exitCode = process.waitFor()
            // show stdout
            process.inputStream.bufferedReader().use { println(it.readText()) }
            // show stderr
            process.errorStream.bufferedReader().use { println(it.readText()) }
            exitCode == 0
        } catch (e: Exception) {
            e.printStackTrace()
            false
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy