Quote VS Code Dosh proxy commands on Windows
ci / test (push) Canceled after 0s
ci / fuzz-smoke (push) Canceled after 0s
ci / macos-client (macos-aarch64, macos-14) (push) Canceled after 0s
ci / macos-client (macos-x86_64, macos-13) (push) Canceled after 0s
ci / windows-client (push) Canceled after 0s
ci / package-release (linux-x86_64, ubuntu-latest, , , ) (push) Canceled after 0s
ci / package-release (macos-aarch64, macos-14, , , ) (push) Canceled after 0s
ci / package-release (macos-x86_64, macos-13, , , ) (push) Canceled after 0s
ci / package-release (windows-aarch64, windows-latest, aarch64, windows, aarch64-pc-windows-msvc) (push) Canceled after 0s
ci / package-release (windows-x86_64, windows-latest, , , ) (push) Canceled after 0s
ci / remote-bench (push) Canceled after 0s
ci / publish-gitea-release (push) Canceled after 0s
ci / test (push) Canceled after 0s
ci / fuzz-smoke (push) Canceled after 0s
ci / macos-client (macos-aarch64, macos-14) (push) Canceled after 0s
ci / macos-client (macos-x86_64, macos-13) (push) Canceled after 0s
ci / windows-client (push) Canceled after 0s
ci / package-release (linux-x86_64, ubuntu-latest, , , ) (push) Canceled after 0s
ci / package-release (macos-aarch64, macos-14, , , ) (push) Canceled after 0s
ci / package-release (macos-x86_64, macos-13, , , ) (push) Canceled after 0s
ci / package-release (windows-aarch64, windows-latest, aarch64, windows, aarch64-pc-windows-msvc) (push) Canceled after 0s
ci / package-release (windows-x86_64, windows-latest, , , ) (push) Canceled after 0s
ci / remote-bench (push) Canceled after 0s
ci / publish-gitea-release (push) Canceled after 0s
This commit is contained in:
@@ -1,22 +1,37 @@
|
||||
const vscode = require('vscode');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
|
||||
let vscode;
|
||||
try {
|
||||
vscode = require('vscode');
|
||||
} catch (_) {
|
||||
vscode = undefined;
|
||||
}
|
||||
|
||||
function requireVscode() {
|
||||
if (!vscode) {
|
||||
throw new Error('VS Code API is only available inside VS Code');
|
||||
}
|
||||
return vscode;
|
||||
}
|
||||
|
||||
function activate(context) {
|
||||
const api = requireVscode();
|
||||
context.subscriptions.push(
|
||||
vscode.commands.registerCommand('dosh.openRemote', openRemote),
|
||||
vscode.commands.registerCommand('dosh.configureHost', configureHostCommand),
|
||||
vscode.commands.registerCommand('dosh.showSshConfig', showSshConfig)
|
||||
api.commands.registerCommand('dosh.openRemote', openRemote),
|
||||
api.commands.registerCommand('dosh.configureHost', configureHostCommand),
|
||||
api.commands.registerCommand('dosh.showSshConfig', showSshConfig)
|
||||
);
|
||||
}
|
||||
|
||||
async function openRemote() {
|
||||
const api = requireVscode();
|
||||
const configured = await configureHost();
|
||||
if (!configured) {
|
||||
return;
|
||||
}
|
||||
const remotePath = await vscode.window.showInputBox({
|
||||
const remotePath = await api.window.showInputBox({
|
||||
title: 'Remote path',
|
||||
prompt: 'Path to open on the remote host',
|
||||
value: '~'
|
||||
@@ -25,22 +40,24 @@ async function openRemote() {
|
||||
return;
|
||||
}
|
||||
const suffix = remotePath ? `/${remotePath.replace(/^\/+/, '')}` : '';
|
||||
await vscode.commands.executeCommand(
|
||||
await api.commands.executeCommand(
|
||||
'vscode.openFolder',
|
||||
vscode.Uri.parse(`vscode-remote://ssh-remote+${configured.alias}${suffix}`),
|
||||
api.Uri.parse(`vscode-remote://ssh-remote+${configured.alias}${suffix}`),
|
||||
{ forceNewWindow: true }
|
||||
);
|
||||
}
|
||||
|
||||
async function configureHostCommand() {
|
||||
const api = requireVscode();
|
||||
const configured = await configureHost();
|
||||
if (configured) {
|
||||
vscode.window.showInformationMessage(`Dosh Remote-SSH host ready: ${configured.alias}`);
|
||||
api.window.showInformationMessage(`Dosh Remote-SSH host ready: ${configured.alias}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function configureHost() {
|
||||
const host = await vscode.window.showInputBox({
|
||||
const api = requireVscode();
|
||||
const host = await api.window.showInputBox({
|
||||
title: 'Dosh host',
|
||||
prompt: 'Dosh host alias, e.g. palav',
|
||||
ignoreFocusOut: true
|
||||
@@ -48,12 +65,12 @@ async function configureHost() {
|
||||
if (!host) {
|
||||
return undefined;
|
||||
}
|
||||
const user = await vscode.window.showInputBox({
|
||||
const user = await api.window.showInputBox({
|
||||
title: 'Remote SSH user',
|
||||
prompt: 'Optional. Leave blank to let SSH config decide.',
|
||||
ignoreFocusOut: true
|
||||
});
|
||||
const config = vscode.workspace.getConfiguration('dosh');
|
||||
const config = api.workspace.getConfiguration('dosh');
|
||||
const alias = `dosh-${safeAlias(host)}`;
|
||||
const block = sshBlock({
|
||||
alias,
|
||||
@@ -62,7 +79,8 @@ async function configureHost() {
|
||||
executable: config.get('executable') || 'dosh',
|
||||
targetHost: config.get('targetHost') || '127.0.0.1',
|
||||
targetPort: Number(config.get('targetPort') || 22),
|
||||
doshPort: Number(config.get('doshPort') || 0)
|
||||
doshPort: Number(config.get('doshPort') || 0),
|
||||
platform: process.platform
|
||||
});
|
||||
const sshDir = path.join(os.homedir(), '.ssh');
|
||||
fs.mkdirSync(sshDir, { recursive: true });
|
||||
@@ -74,11 +92,12 @@ async function configureHost() {
|
||||
}
|
||||
|
||||
function sshBlock(options) {
|
||||
let proxy = shellQuote(options.executable);
|
||||
const platform = options.platform || process.platform;
|
||||
let proxy = sshConfigWord(options.executable, platform);
|
||||
if (options.doshPort > 0) {
|
||||
proxy += ` --dosh-port ${options.doshPort}`;
|
||||
}
|
||||
proxy += ` proxy-stdio ${shellQuote(options.host)} %h %p`;
|
||||
proxy += ` proxy-stdio ${sshConfigWord(options.host, platform)} %h %p`;
|
||||
const lines = [
|
||||
`# BEGIN DOSH ${options.alias}`,
|
||||
`Host ${options.alias}`,
|
||||
@@ -139,15 +158,16 @@ function upsertBlock(configPath, alias, block) {
|
||||
}
|
||||
|
||||
async function showSshConfig() {
|
||||
const config = vscode.workspace.getConfiguration('dosh');
|
||||
const api = requireVscode();
|
||||
const config = api.workspace.getConfiguration('dosh');
|
||||
const sshDir = path.join(os.homedir(), '.ssh');
|
||||
const generatedPath = config.get('generatedSshConfig') || path.join(sshDir, 'config.dosh');
|
||||
if (!fs.existsSync(generatedPath)) {
|
||||
vscode.window.showWarningMessage('No generated Dosh SSH config yet.');
|
||||
api.window.showWarningMessage('No generated Dosh SSH config yet.');
|
||||
return;
|
||||
}
|
||||
const doc = await vscode.workspace.openTextDocument(generatedPath);
|
||||
await vscode.window.showTextDocument(doc);
|
||||
const doc = await api.workspace.openTextDocument(generatedPath);
|
||||
await api.window.showTextDocument(doc);
|
||||
}
|
||||
|
||||
function safeAlias(value) {
|
||||
@@ -162,9 +182,43 @@ function shellQuote(value) {
|
||||
return `'${value.replace(/'/g, `'\\''`)}'`;
|
||||
}
|
||||
|
||||
function sshConfigWord(value, platform = process.platform) {
|
||||
if (platform === 'win32') {
|
||||
return windowsCommandWord(value);
|
||||
}
|
||||
return shellQuote(value);
|
||||
}
|
||||
|
||||
function windowsCommandWord(value) {
|
||||
let out = '"';
|
||||
let backslashes = 0;
|
||||
for (const ch of value) {
|
||||
if (ch === '\\') {
|
||||
backslashes += 1;
|
||||
continue;
|
||||
}
|
||||
if (ch === '"') {
|
||||
out += '\\'.repeat(backslashes * 2 + 1);
|
||||
out += '"';
|
||||
backslashes = 0;
|
||||
continue;
|
||||
}
|
||||
out += '\\'.repeat(backslashes);
|
||||
backslashes = 0;
|
||||
out += ch;
|
||||
}
|
||||
out += '\\'.repeat(backslashes * 2);
|
||||
out += '"';
|
||||
return out;
|
||||
}
|
||||
|
||||
function deactivate() {}
|
||||
|
||||
module.exports = {
|
||||
activate,
|
||||
deactivate
|
||||
deactivate,
|
||||
sshBlock,
|
||||
shellQuote,
|
||||
sshConfigWord,
|
||||
windowsCommandWord
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user