feat: show SSH:project name in status bar
This commit is contained in:
@@ -16,18 +16,36 @@ export default class RemoteContainer {
|
||||
this.user = user
|
||||
}
|
||||
|
||||
async firstOpenProject(hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
|
||||
await this.firstConnect(hostname, username, port, context)
|
||||
/**
|
||||
* 第一次打开远程项目
|
||||
* @param host 项目名称
|
||||
* @param hostname ip
|
||||
* @param port
|
||||
* @param username
|
||||
* @param path
|
||||
* @param context 用于支持远程项目环境
|
||||
*/
|
||||
async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
|
||||
await this.firstConnect(host, hostname, username, port, context)
|
||||
.then((res) => {
|
||||
if (res === 'success') {
|
||||
// only success then open folder
|
||||
this.openRemoteFolder(hostname, port, username, path);
|
||||
this.openRemoteFolder(host, port, username, path);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 本地/远程项目环境下,第一次连接其他项目
|
||||
* @param host 项目名称
|
||||
* @param hostname ip
|
||||
* @param username
|
||||
* @param port
|
||||
* @param context 用于支持远程项目环境
|
||||
* @returns 成功返回success
|
||||
*/
|
||||
// connect with key
|
||||
async firstConnect(hostname: string, username: string, port: number, context: vscode.ExtensionContext): Promise<string> {
|
||||
async firstConnect(host: string, hostname: string, username: string, port: number, context: vscode.ExtensionContext): Promise<string> {
|
||||
return new Promise(async (resolve) => {
|
||||
const ssh = new NodeSSH();
|
||||
vscode.window.withProgress({
|
||||
@@ -87,11 +105,10 @@ export default class RemoteContainer {
|
||||
await ssh.dispose();
|
||||
|
||||
// ssh信息存储到ssh config file中
|
||||
// 远程环境,利用global state中记录的localSystemName来决定执行的命令
|
||||
// 远程项目环境,利用global state中记录的localSystemName来决定执行的命令
|
||||
const localSystemName = utils.getLocalSystemName(context)
|
||||
const localSSHConfigPath = utils.getLocalSSHConfigPath(context);
|
||||
const privateKeyPath = this.user.getLocalUserPrivateKeyPath();
|
||||
const host = `${hostname}-${port}` // host: hostname-port
|
||||
const newSShConfigContent =
|
||||
`\nHost ${host}\n HostName ${hostname}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
||||
let commandToAppend: string;
|
||||
@@ -164,7 +181,7 @@ export default class RemoteContainer {
|
||||
await ssh.dispose();
|
||||
|
||||
// only connect successfully then save the host info
|
||||
await this.storeHostInfo(hostname, port, username)
|
||||
await this.storeProjectSSHInfo(host, hostname, port, username)
|
||||
|
||||
resolve('success')
|
||||
} catch (error) {
|
||||
@@ -176,7 +193,14 @@ export default class RemoteContainer {
|
||||
});
|
||||
}
|
||||
|
||||
async storeHostInfo(hostname: string, port: number, username: string): Promise<void> {
|
||||
/**
|
||||
* 本地环境,保存项目的ssh连接信息
|
||||
* @param host
|
||||
* @param hostname
|
||||
* @param port
|
||||
* @param username
|
||||
*/
|
||||
async storeProjectSSHInfo(host: string, hostname: string, port: number, username: string): Promise<void> {
|
||||
const sshConfigPath = path.join(os.homedir(), '.ssh', 'config');
|
||||
// check if the host and related info exist in local ssh config file before saving
|
||||
var canAppendSSHConfig = true
|
||||
@@ -184,8 +208,7 @@ export default class RemoteContainer {
|
||||
var reader = rd.createInterface(fs.createReadStream(sshConfigPath))
|
||||
|
||||
for await (const line of reader) {
|
||||
// host format: hostname-port
|
||||
if (line.includes(`Host ${hostname}-${port}`)) {
|
||||
if (line.includes(`Host ${host}`)) {
|
||||
// the container ssh info exists
|
||||
canAppendSSHConfig = false
|
||||
break;
|
||||
@@ -195,7 +218,6 @@ export default class RemoteContainer {
|
||||
|
||||
if (canAppendSSHConfig) {
|
||||
// save the host to the local ssh config file
|
||||
const host = `${hostname}-${port}` // host: hostname-port
|
||||
const privateKeyPath = this.user.getUserPrivateKeyPath();
|
||||
const newSShConfigContent =
|
||||
`\nHost ${host}\n HostName ${hostname}\n Port ${port}\n User ${username}\n PreferredAuthentications publickey\n IdentityFile ${privateKeyPath}\n `;
|
||||
@@ -205,21 +227,24 @@ export default class RemoteContainer {
|
||||
}
|
||||
|
||||
|
||||
openRemoteFolder(hostname: string, port: number, username: string, path: string): void {
|
||||
/**
|
||||
* 仅支持已经成功连接,并在ssh config file中存储ssh信息的项目连接。
|
||||
*
|
||||
* @host 表示project name
|
||||
*/
|
||||
openRemoteFolder(host: string, port: number, username: string, path: string): void {
|
||||
if (vscode.env.remoteName) {
|
||||
// 远程环境,打开local terminal
|
||||
vscode.commands.executeCommand('workbench.action.terminal.newLocal').then(() => {
|
||||
// 获取最后一个打开的终端实例
|
||||
const terminal = vscode.window.terminals[vscode.window.terminals.length - 1];
|
||||
if (terminal) {
|
||||
var host = `${hostname}-${port}`
|
||||
// 在新窗口打开
|
||||
terminal.sendText(`code --remote ssh-remote+${username}@${host}:${port} ${path} --new-window`)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// 本地环境
|
||||
var host = `${hostname}-${port}`
|
||||
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
||||
terminal.show(true);
|
||||
// 在原窗口打开
|
||||
@@ -229,8 +254,15 @@ export default class RemoteContainer {
|
||||
}
|
||||
|
||||
|
||||
export async function openProjectWithoutLogging(host: string, port: number, username: string, path: string): Promise<void> {
|
||||
const command = `code --remote ssh-remote+${username}@${host}:${port} ${path} --reuse-window`
|
||||
/**
|
||||
* 打开项目(无须插件登录)
|
||||
* @param hostname 表示ip
|
||||
* @param port
|
||||
* @param username
|
||||
* @param path
|
||||
*/
|
||||
export async function openProjectWithoutLogging(hostname: string, port: number, username: string, path: string): Promise<void> {
|
||||
const command = `code --remote ssh-remote+${username}@${hostname}:${port} ${path} --reuse-window`
|
||||
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
||||
terminal.show(true);
|
||||
terminal.sendText(command);
|
||||
|
Reference in New Issue
Block a user