CORDA-2622 - Fix shell extensions (#4774)

* CORDA-2622 - Fix shell extensions

* CORDA-2622 - Only add auto-completion stuff if enabled
This commit is contained in:
Tommy Lillehagen 2019-02-17 17:16:57 +00:00 committed by GitHub
parent f781edccd4
commit 4434a005dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package net.corda.cliutils
import net.corda.core.internal.*
import net.corda.core.utilities.loggerFor
import org.apache.commons.io.IOUtils
import org.apache.commons.lang.SystemUtils
import picocli.CommandLine
@ -108,7 +109,9 @@ private class ShellExtensionsGenerator(val parent: CordaCliWrapper) {
// Replace any existing alias. There can be only one.
bashSettingsFile.addOrReplaceIfStartsWith("alias ${parent.alias}", command)
val completionFileCommand = "for bcfile in ~/.completion/* ; do . \$bcfile; done"
bashSettingsFile.addIfNotExists(completionFileCommand)
if (generateAutoCompleteFile) {
bashSettingsFile.addIfNotExists(completionFileCommand)
}
bashSettingsFile.updateAndBackupIfNecessary()
// Get zsh settings file
@ -116,7 +119,9 @@ private class ShellExtensionsGenerator(val parent: CordaCliWrapper) {
zshSettingsFile.addIfNotExists("autoload -U +X compinit && compinit")
zshSettingsFile.addIfNotExists("autoload -U +X bashcompinit && bashcompinit")
zshSettingsFile.addOrReplaceIfStartsWith("alias ${parent.alias}", command)
zshSettingsFile.addIfNotExists(completionFileCommand)
if (generateAutoCompleteFile) {
zshSettingsFile.addIfNotExists(completionFileCommand)
}
zshSettingsFile.updateAndBackupIfNecessary()
if (generateAutoCompleteFile) {
@ -129,10 +134,10 @@ private class ShellExtensionsGenerator(val parent: CordaCliWrapper) {
return ExitCodes.SUCCESS
}
private fun declaredBashVersion(): String = execCommand("bash -c 'echo \$BASH_VERSION'")
private fun declaredBashVersion(): String = execCommand("bash", "-c", "echo \$BASH_VERSION")
private fun installedShell(): ShellType {
val path = execCommand("bash -c 'echo \$SHELL'")
val path = execCommand("bash", "-c", "echo \$SHELL").trim()
return when {
path.endsWith("/zsh") -> ShellType.ZSH
path.endsWith("/bash") -> ShellType.BASH
@ -144,9 +149,14 @@ private class ShellExtensionsGenerator(val parent: CordaCliWrapper) {
ZSH, BASH, OTHER
}
private fun execCommand(command: String): String {
val process = ProcessBuilder(command)
return IOUtils.toString(process.start().inputStream, Charsets.UTF_8)
private fun execCommand(vararg commandAndArgs: String): String {
return try {
val process = ProcessBuilder(*commandAndArgs)
IOUtils.toString(process.start().inputStream, Charsets.UTF_8)
} catch (exception: Exception) {
loggerFor<InstallShellExtensionsParser>().warn("Failed to run command: ${commandAndArgs.joinToString(" ")}; $exception")
""
}
}
fun checkForAutoCompleteUpdate() {