refactor: change name: host->hostname

This commit is contained in:
Levi Yan
2025-05-06 23:04:10 +08:00
parent c15b8919ca
commit e4d9d2b3b7

View File

@@ -78,7 +78,7 @@ export default class RemoteContainer {
}); });
} }
async storeHostInfo(host: string, port: number, username: string): Promise<void> { async storeHostInfo(hostname: string, port: number, username: string): Promise<void> {
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config'); const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
// check if the host and related info exist in local ssh config file before saving // check if the host and related info exist in local ssh config file before saving
var canAppendSSHConfig = true var canAppendSSHConfig = true
@@ -86,8 +86,8 @@ export default class RemoteContainer {
var reader = rd.createInterface(fs.createReadStream(sshConfigPath)) var reader = rd.createInterface(fs.createReadStream(sshConfigPath))
for await (const line of reader) { for await (const line of reader) {
// host format: hostname:port // host format: hostname-port
if (line.includes(`Host ${host}-${port}`)) { if (line.includes(`Host ${hostname}-${port}`)) {
// the container ssh info exists // the container ssh info exists
canAppendSSHConfig = false canAppendSSHConfig = false
break; break;
@@ -97,10 +97,10 @@ export default class RemoteContainer {
if (canAppendSSHConfig) { if (canAppendSSHConfig) {
// save the host to the local ssh config file // save the host to the local ssh config file
const hostInConfig = `${host}-${port}` // host format: hostname-port const host = `${hostname}-${port}` // host: hostname-port
const privateKeyPath = this.user.getUserPrivateKeyPath(); const privateKeyPath = this.user.getUserPrivateKeyPath();
const newSShConfigContent = const newSShConfigContent =
`\nHost ${hostInConfig}\n HostName ${host}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `; `\nHost ${host}\n HostName ${hostname}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' }); fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
console.log('Host registered in local ssh config'); console.log('Host registered in local ssh config');
} }