Compare commits
2 Commits
main
...
6cdf60ced1
| Author | SHA1 | Date | |
|---|---|---|---|
| 6cdf60ced1 | |||
| 629a9b4bf7 |
16
src/main.ts
16
src/main.ts
@@ -67,6 +67,7 @@ export class DevStarExtension {
|
|||||||
const accessToken = params.get('access_token');
|
const accessToken = params.get('access_token');
|
||||||
const devstarUsername = params.get('devstar_username');
|
const devstarUsername = params.get('devstar_username');
|
||||||
const rawDevstarDomain = params.get('devstar_domain');
|
const rawDevstarDomain = params.get('devstar_domain');
|
||||||
|
const cmd = params.get('cmd') || 'code'; // 获取 cmd 参数,默认为 code
|
||||||
let devstarDomain = rawDevstarDomain;
|
let devstarDomain = rawDevstarDomain;
|
||||||
if (rawDevstarDomain) {
|
if (rawDevstarDomain) {
|
||||||
try {
|
try {
|
||||||
@@ -123,11 +124,11 @@ export class DevStarExtension {
|
|||||||
// 如果没有用户登录,则直接登录;
|
// 如果没有用户登录,则直接登录;
|
||||||
const res = await this.user.login(accessToken, devstarUsername)
|
const res = await this.user.login(accessToken, devstarUsername)
|
||||||
if (res === 'ok') {
|
if (res === 'ok') {
|
||||||
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
|
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context, cmd)
|
||||||
}
|
}
|
||||||
} else if (devstarUsername === this.user.getUsernameFromLocal()) {
|
} else if (devstarUsername === this.user.getUsernameFromLocal()) {
|
||||||
// 如果同用户已经登录,则忽略,直接打开项目
|
// 如果同用户已经登录,则忽略,直接打开项目
|
||||||
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
|
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context, cmd)
|
||||||
} else {
|
} else {
|
||||||
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
|
// 如果不是同用户,可以选择切换用户,或者不切换登录用户,直接打开容器
|
||||||
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
|
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
|
||||||
@@ -136,14 +137,14 @@ export class DevStarExtension {
|
|||||||
// 如果没有用户登录,则直接登录;
|
// 如果没有用户登录,则直接登录;
|
||||||
const res = await this.user.login(accessToken, devstarUsername)
|
const res = await this.user.login(accessToken, devstarUsername)
|
||||||
if (res === 'ok') {
|
if (res === 'ok') {
|
||||||
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context)
|
await this.remoteContainer.firstOpenProject(containerHost, containerHostname, containerPort, containerUsername, projectPath, this.context, cmd)
|
||||||
}
|
}
|
||||||
} else if (selection === 'No') {
|
} else if (selection === 'No') {
|
||||||
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
|
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath, cmd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath);
|
await openProjectWithoutLogging(containerHost, containerPort, containerUsername, projectPath, cmd);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
vscode.window.showErrorMessage('Missing required parameters.');
|
vscode.window.showErrorMessage('Missing required parameters.');
|
||||||
@@ -160,6 +161,7 @@ export class DevStarExtension {
|
|||||||
const username = params.get('username');
|
const username = params.get('username');
|
||||||
const path = params.get('path');
|
const path = params.get('path');
|
||||||
const devstarDomain = params.get('devstar_domain')
|
const devstarDomain = params.get('devstar_domain')
|
||||||
|
const cmd = params.get('cmd') || 'code'; // 获取 cmd 参数,默认为 code
|
||||||
|
|
||||||
if (host && hostname && port && username && path) {
|
if (host && hostname && port && username && path) {
|
||||||
const container_host = host;
|
const container_host = host;
|
||||||
@@ -179,10 +181,10 @@ export class DevStarExtension {
|
|||||||
// 将devstar domain存在global state中
|
// 将devstar domain存在global state中
|
||||||
context.globalState.update('devstarDomain', devstarDomain)
|
context.globalState.update('devstarDomain', devstarDomain)
|
||||||
|
|
||||||
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context, cmd)
|
||||||
} else {
|
} else {
|
||||||
// devstarDomain参数不存在,则不存储,使用默认用户配置
|
// devstarDomain参数不存在,则不存储,使用默认用户配置
|
||||||
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context)
|
await this.remoteContainer.firstOpenProject(container_host, container_hostname, container_port, container_username, project_path, this.context, cmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export default class RemoteContainer {
|
|||||||
/**
|
/**
|
||||||
* 第一次打开远程项目
|
* 第一次打开远程项目
|
||||||
*/
|
*/
|
||||||
async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext) {
|
async firstOpenProject(host: string, hostname: string, port: number, username: string, path: string, context: vscode.ExtensionContext, cmd: string = 'code') {
|
||||||
if (vscode.env.remoteName) {
|
if (vscode.env.remoteName) {
|
||||||
try {
|
try {
|
||||||
await vscode.commands.executeCommand('workbench.action.terminal.newLocal');
|
await vscode.commands.executeCommand('workbench.action.terminal.newLocal');
|
||||||
@@ -76,7 +76,7 @@ export default class RemoteContainer {
|
|||||||
await this.firstConnect(host, hostname, username, port, path)
|
await this.firstConnect(host, hostname, username, port, path)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
if (res === 'success') {
|
if (res === 'success') {
|
||||||
this.openRemoteFolder(host, port, username, path, context);
|
this.openRemoteFolder(host, port, username, path, context, cmd);
|
||||||
} else {
|
} else {
|
||||||
vscode.window.showErrorMessage('首次连接容器失败,请检查网络和容器状态。');
|
vscode.window.showErrorMessage('首次连接容器失败,请检查网络和容器状态。');
|
||||||
}
|
}
|
||||||
@@ -350,9 +350,9 @@ export default class RemoteContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* local env
|
* local env - 使用 Extension API 打开远程文件夹
|
||||||
*/
|
*/
|
||||||
async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext): Promise<void> {
|
async openRemoteFolder(host: string, port: number, _username: string, path: string, context: vscode.ExtensionContext, _cmd: string = 'code'): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const sshConfig = await this.getSSHConfig(host);
|
const sshConfig = await this.getSSHConfig(host);
|
||||||
if (sshConfig) {
|
if (sshConfig) {
|
||||||
@@ -365,12 +365,18 @@ export default class RemoteContainer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
// 使用 VSCode Extension API 打开远程连接
|
||||||
terminal.show(true);
|
// 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效
|
||||||
|
// 格式: vscode-remote://ssh-remote+host/path
|
||||||
|
// 注意:不在 URI 中指定用户名,让 SSH config 中的 User root 配置生效
|
||||||
|
const remoteUri = vscode.Uri.parse(
|
||||||
|
`vscode-remote://ssh-remote+${host}${path}`
|
||||||
|
);
|
||||||
|
|
||||||
const command = `code --remote ssh-remote+root@${host}:${port} ${path} --reuse-window`;
|
// 尝试使用 vscode.openFolder 命令
|
||||||
|
await vscode.commands.executeCommand('vscode.openFolder', remoteUri, {
|
||||||
terminal.sendText(command);
|
forceNewWindow: false
|
||||||
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
||||||
@@ -459,15 +465,22 @@ export default class RemoteContainer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 打开项目(无须插件登录)
|
* 打开项目(无须插件登录)- 使用 Extension API
|
||||||
*/
|
*/
|
||||||
export async function openProjectWithoutLogging(hostname: string, port: number, username: string, path: string): Promise<void> {
|
export async function openProjectWithoutLogging(host: string, _port: number, _username: string, path: string, _cmd: string = 'code'): Promise<void> {
|
||||||
const command = `code --remote ssh-remote+${username}@${hostname}:${port} ${path} --reuse-window`;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let terminal = vscode.window.activeTerminal || vscode.window.createTerminal(`Ext Terminal`);
|
// 使用 VSCode Extension API 打开远程连接
|
||||||
terminal.show(true);
|
// 使用 SSH config 中的 Host 别名,让 SSH config 的用户配置生效
|
||||||
terminal.sendText(command);
|
// 格式: vscode-remote://ssh-remote+host/path
|
||||||
|
const remoteUri = vscode.Uri.parse(
|
||||||
|
`vscode-remote://ssh-remote+${host}${path}`
|
||||||
|
);
|
||||||
|
|
||||||
|
// 使用 vscode.openFolder 命令
|
||||||
|
await vscode.commands.executeCommand('vscode.openFolder', remoteUri, {
|
||||||
|
forceNewWindow: false
|
||||||
|
});
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
const errorMessage = error instanceof Error ? error.message : '未知错误';
|
||||||
vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);
|
vscode.window.showErrorMessage(`无登录打开项目失败: ${errorMessage}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user