forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh.ts
More file actions
60 lines (54 loc) · 1.98 KB
/
ssh.ts
File metadata and controls
60 lines (54 loc) · 1.98 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { readFileSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import Logger from '../../common/logger';
import { baseResolver, chainResolvers, ConfigResolver, resolverFromConfig, sshParse } from '../browser/ssh';
export class Resolvers {
static default = chainResolvers(baseResolver, resolverFromConfigFile());
static fromConfig(conf: string) {
return chainResolvers(baseResolver, resolverFromConfig(conf));
}
static current = Resolvers.default;
}
/**
* Parse and resolve an SSH url. Resolves host aliases using the configuration
* specified by ~/.ssh/config, if present.
*
* Examples:
*
* resolve("git@github.com:Microsoft/vscode")
* {
* Host: 'github.com',
* HostName: 'github.com',
* User: 'git',
* path: 'Microsoft/vscode',
* }
*
* resolve("hub:queerviolet/vscode", resolverFromConfig("Host hub\n HostName github.com\n User git\n"))
* {
* Host: 'hub',
* HostName: 'github.com',
* User: 'git',
* path: 'queerviolet/vscode',
* }
*
* @param {string} url the url to parse
* @param {ConfigResolver?} resolveConfig ssh config resolver (default: from ~/.ssh/config)
* @returns {Config}
*/
export const resolve = (url: string, resolveConfig = Resolvers.current) => {
const config = sshParse(url);
return config && resolveConfig(config);
};
function resolverFromConfigFile(configPath = join(homedir(), '.ssh', 'config')): ConfigResolver | undefined {
try {
const config = readFileSync(configPath).toString();
return resolverFromConfig(config);
} catch (error) {
Logger.warn(`${configPath}: ${error.message}`);
}
}