feat: copy public key display name (#186)

This commit is contained in:
hstyi
2025-02-09 16:56:10 +08:00
committed by GitHub
parent 6806c26028
commit 724c5d2632
2 changed files with 19 additions and 8 deletions

View File

@@ -198,12 +198,12 @@ class KeyManagerPanel : JPanel(BorderLayout()) {
private fun sshCopyId(evt: AnActionEvent) { private fun sshCopyId(evt: AnActionEvent) {
val windowScope = evt.getData(DataProviders.WindowScope) ?: return val windowScope = evt.getData(DataProviders.WindowScope) ?: return
val keyPairs = keyPairTable.selectedRows.map { keyPairTableModel.getOhKeyPair(it) } val keyPairs = keyPairTable.selectedRows.map { keyPairTableModel.getOhKeyPair(it) }
val publicKeys = mutableListOf<String>() val publicKeys = mutableListOf<Pair<String, String>>()
for (keyPair in keyPairs) { for (keyPair in keyPairs) {
val publicKey = OhKeyPairKeyPairProvider.generateKeyPair(keyPair).public val publicKey = OhKeyPairKeyPairProvider.generateKeyPair(keyPair).public
val baos = ByteArrayOutputStream() val baos = ByteArrayOutputStream()
OpenSSHKeyPairResourceWriter.INSTANCE.writePublicKey(publicKey, keyPair.name, baos) OpenSSHKeyPairResourceWriter.INSTANCE.writePublicKey(publicKey, keyPair.name, baos)
publicKeys.add(baos.toString(Charsets.UTF_8)) publicKeys.add(Pair(keyPair.name, baos.toString(Charsets.UTF_8)))
} }
if (publicKeys.isEmpty()) { if (publicKeys.isEmpty()) {

View File

@@ -25,7 +25,8 @@ class SSHCopyIdDialog(
owner: Window, owner: Window,
private val windowScope: WindowScope, private val windowScope: WindowScope,
private val hosts: List<Host>, private val hosts: List<Host>,
private val publicKeys: List<String>, // key: name , value: public key
private val publicKeys: List<Pair<String, String>>,
) : DialogWrapper(owner) { ) : DialogWrapper(owner) {
companion object { companion object {
@@ -114,6 +115,9 @@ class SSHCopyIdDialog(
var mySession: ClientSession? = null var mySession: ClientSession? = null
val timeout = Duration.ofMinutes(1) val timeout = Duration.ofMinutes(1)
// 获取公钥名称最长的
val publicKeyNameLength = publicKeys.maxOfOrNull { it.first.length } ?: 0
for (index in hosts.indices) { for (index in hosts.indices) {
if (!coroutineScope.isActive) { if (!coroutineScope.isActive) {
return return
@@ -125,15 +129,18 @@ class SSHCopyIdDialog(
terminal.getDocument().newline() terminal.getDocument().newline()
} }
for (j in publicKeys.indices) { for ((j, e) in publicKeys.withIndex()) {
if (!coroutineScope.isActive) { if (!coroutineScope.isActive) {
return return
} }
val publicKey = publicKeys[j] val publicKeyName = e.first.padEnd(publicKeyNameLength, ' ')
val publicKey = e.second
withContext(Dispatchers.Swing) { withContext(Dispatchers.Swing) {
terminal.write("\t[${cyan(j + 1)}/${cyan(publicKeys.size)}] ${I18n.getString("termora.transport.sftp.connecting")}") // @formatter:off
terminal.write("\t[${cyan(j + 1)}/${cyan(publicKeys.size)}] $publicKeyName ${I18n.getString("termora.transport.sftp.connecting")}")
// @formatter:on
} }
try { try {
@@ -152,12 +159,16 @@ class SSHCopyIdDialog(
} }
withContext(Dispatchers.Swing) { withContext(Dispatchers.Swing) {
terminal.getDocument().eraseInLine(2) terminal.getDocument().eraseInLine(2)
terminal.write("\r\t[${cyan(j + 1)}/${cyan(publicKeys.size)}] ${green(I18n.getString("termora.keymgr.ssh-copy-id.successful"))}") // @formatter:off
terminal.write("\r\t[${cyan(j + 1)}/${cyan(publicKeys.size)}] $publicKeyName ${green(I18n.getString("termora.keymgr.ssh-copy-id.successful"))}")
// @formatter:on
} }
} catch (e: Exception) { } catch (e: Exception) {
withContext(Dispatchers.Swing) { withContext(Dispatchers.Swing) {
terminal.getDocument().eraseInLine(2) terminal.getDocument().eraseInLine(2)
terminal.write("\r\t[${cyan(j + 1)}/${cyan(publicKeys.size)}] ${red("${I18n.getString("termora.keymgr.ssh-copy-id.failed")}: ${e.message}")}") // @formatter:off
terminal.write("\r\t[${cyan(j + 1)}/${cyan(publicKeys.size)}] $publicKeyName ${red("${I18n.getString("termora.keymgr.ssh-copy-id.failed")}: ${e.message}")}")
// @formatter:on
} }
} finally { } finally {
IOUtils.closeQuietly(mySession) IOUtils.closeQuietly(mySession)