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

app.softwork.kobol.gradle.sshClient.kt Maven / Gradle / Ivy

There is a newer version: 0.2.15
Show newest version
package app.softwork.kobol.gradle

import com.jcraft.jsch.agentproxy.*
import net.schmizz.sshj.*
import org.gradle.api.logging.*

internal fun sshClient(host: String, user: String, action: SSHClient.() -> Unit) {
    SSHClient().use { ssh ->
        ssh.loadKnownHosts()
        ssh.connect(host)
        ssh.auth(user, PAgentProxy.authAgents())
        ssh.action()
    }
}

internal fun SSHClient.exec(cmd: String, logger: Logger) {
    logger.info(cmd)
    val (output, result) = startSession().use { session ->
        val result = session.exec(cmd)
        result.inputStream.reader().use { it.readText() } to result
    }
    val exitCode: Int? = result.exitStatus
    if (exitCode != 0) {
        val error = result.errorStream.reader().use { it.readText() }
        logger.error("$cmd\n$error " + (result.exitErrorMessage ?: ""))
    }
    if (output.isNotBlank()) {
        logger.quiet(output)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy