fun runProcess()

in src/aws/install-aws-cli/src.main.kts [364:401]


    fun runProcess(
        command: List<String>,
        workingDir: File,
        options: RunOptions = RunOptions(),
    ): ProcessResult? = runBlocking {
        if (!options.isSilent) {
            println("Starting: ${command.joinToString(" ")}")
            println("In directory: ${workingDir.absolutePath}")
        }
        try {
            val process = ProcessBuilder(command).directory(workingDir).redirectErrorStream(false).start()

            val stdoutDeferred = readLines(process.inputStream, options.isSilent, false)
            val stderrDeferred = readLines(process.errorStream, options.isSilent, true)

            if (!process.waitFor(options.executionTimeout.inWholeMilliseconds, TimeUnit.MILLISECONDS)) {
                if (!options.isSilent) {
                    System.err.println("Execution timeout exceeded")
                }
                process.destroy()
                process.waitFor(5, TimeUnit.SECONDS)
                if (process.isAlive) {
                    process.destroyForcibly()
                }
                return@runBlocking null
            }
            val stdout = stdoutDeferred.await()
            val stderr = stderrDeferred.await()

            ProcessResult(process.exitValue(), stdout, stderr)
        } catch (e: Throwable) {
            if (!options.isSilent) {
                System.err.println("Failed to execute command")
                System.err.println(e.stackTraceToString())
            }
            return@runBlocking null
        }
    }