-
Notifications
You must be signed in to change notification settings - Fork 8
/
ssh.ts
47 lines (39 loc) · 1.42 KB
/
ssh.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { readFile, exists } from 'fs';
import * as vscode from 'vscode';
import { promisify } from 'util';
import * as sshConfig from 'ssh-config';
export async function resolveSSHHostName(host: string): Promise<string> {
// get path from remote-ssh-extension config file
const remoteSettingsPath: string | undefined = vscode.workspace
.getConfiguration('remote.SSH')
.get('configFile');
if (!remoteSettingsPath) {
return host;
}
const pathExists = await promisify(exists)(remoteSettingsPath);
if (!pathExists) {
throw Error('Expected remote ssh settings file does not exist: ' + remoteSettingsPath);
}
const rawString = (await promisify(readFile)(remoteSettingsPath))?.toString();
if (!rawString) {
throw Error('Could not read remote ssh settings file');
}
// parse content
const config = sshConfig.parse(rawString);
const settings = config.find((x) => x.param.toLowerCase() === 'host' && x.value === host);
let resolvedHost = '';
if (settings) {
const hostname = settings.config.find((x) => x.param.toLowerCase() === 'hostname');
if (hostname && hostname.value) {
resolvedHost += `${hostname.value}`;
}
const user = settings.config.find((x) => x.param.toLowerCase() === 'user');
if (user && user.value && hostname?.value) {
resolvedHost = `${user.value}@` + resolvedHost;
}
}
if (resolvedHost === '') {
resolvedHost = host;
}
return resolvedHost;
}