refactor: open remote folder also need username, port now and change the naming format of host in config

This commit is contained in:
Levi Yan
2024-10-28 19:46:26 +08:00
parent 76d713b4f1
commit 99e1693573
2 changed files with 8 additions and 7 deletions

View File

@@ -55,19 +55,18 @@ export default class DSHome {
break;
}
case 'firstOpenRemoteFolder':
const path = data.path
await this.remoteContainer.firstConnect(data.host, data.username, data.port)
.then((_res) => {
if (_res == 'success') {
// only success then open folder
this.remoteContainer.openRemoteFolder(data.host, path);
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
}
}).catch(error => {
console.error(`Failed to connect ${data.host}: `, error)
})
break;
case 'openRemoteFolder':
this.remoteContainer.openRemoteFolder(data.host, data.path);
this.remoteContainer.openRemoteFolder(data.host, data.username, data.port, data.path);
break;
// ----------------- not frequent -----------------------
case 'setUserToken':

View File

@@ -128,9 +128,10 @@ export default class RemoteContainer {
if (canAppendSSHConfig) {
// save the host to the local ssh config file
const hostInConfig = `${host}:${port}` // host format: hostname:port
const hostInConfig = `${host}-${port}` // host format: hostname-port
const privateKeyPath = this.user.getUserPrivateKeyPath();
const newSShConfigContent =
`\nHost ${hostInConfig}\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 ${privateKeyPath}\n `;
fs.writeFileSync(sshConfigPath, newSShConfigContent, { encoding: 'utf8', flag: 'a' });
console.log('Host registered in local ssh config');
}
@@ -141,8 +142,9 @@ export default class RemoteContainer {
}
openRemoteFolder(host: string, path: string): void {
const command = `code --remote ssh-remote+${host} ${path} --reuse-window`
openRemoteFolder(host: string, username:string ,port:number, path: string): void {
var host = `${host}-${port}`
const command = `code --remote ssh-remote+${username}@${host} ${path} --reuse-window`
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
terminal.show(true);
terminal.sendText(command);