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