diff --git a/src/remote-container.ts b/src/remote-container.ts index 394bc0f..f0403d9 100644 --- a/src/remote-container.ts +++ b/src/remote-container.ts @@ -108,29 +108,23 @@ export default class RemoteContainer { // check if the host and related info exist in local ssh config file before saving var canAppendSSHConfig = true if (fs.existsSync(sshConfigPath)) { - var hasHost = false; - var hasPort = false; var reader = rd.createInterface(fs.createReadStream(sshConfigPath)) - reader.on("line", (l:string) => { - if (l.includes(`HostName ${host}`)) { - hasHost = true - } - if (l.includes(`Port ${port}`)) { - hasPort = true + for await (const line of reader) { + // host format: hostname:port + if (line.includes(`Host ${host}:${port}`)) { + // the container ssh info exists + canAppendSSHConfig = false + break; } - }) - - if (hasHost && hasPort) { - // the container ssh info exists - canAppendSSHConfig = false } } if (canAppendSSHConfig) { // save the host to the local ssh config file + const hostInConfig = `${host}:${port}` // host format: hostname:port const newSShConfigContent = - `\nHost ${host}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `; + `\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ~/.ssh/id_rsa\n `; fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' }); console.log('Host registered in local ssh config'); }