2019-05-18 05:49:53 +00:00
|
|
|
import URL from 'url';
|
2018-07-06 05:26:36 +00:00
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
//TODO: add known properties
|
|
|
|
interface IPlatformConfig {
|
|
|
|
[prop: string]: any;
|
|
|
|
name?: string;
|
|
|
|
endpoint?: string;
|
|
|
|
}
|
|
|
|
interface IDict<T> {
|
|
|
|
[key: string]: T;
|
|
|
|
}
|
|
|
|
const platforms: IDict<IPlatformConfig> = {};
|
|
|
|
const hostsOnly: IDict<IPlatformConfig> = {};
|
2018-07-06 05:26:36 +00:00
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
export function update(params: IPlatformConfig) {
|
2018-07-06 05:26:36 +00:00
|
|
|
const { platform } = params;
|
|
|
|
if (!platform) {
|
2018-10-15 11:37:08 +00:00
|
|
|
if (params.endpoint) {
|
|
|
|
const { host } = URL.parse(params.endpoint);
|
2019-05-18 05:49:53 +00:00
|
|
|
hostsOnly[host!] = params;
|
2018-10-15 11:37:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
throw new Error(
|
|
|
|
'Failed to set configuration: no platform or endpoint specified'
|
|
|
|
);
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
2019-05-20 08:59:30 +00:00
|
|
|
const config = { ...params };
|
2018-07-06 05:26:36 +00:00
|
|
|
const { endpoint } = config;
|
|
|
|
if (!endpoint) {
|
2018-09-13 03:31:18 +00:00
|
|
|
// istanbul ignore if
|
|
|
|
if (platform === 'docker') {
|
|
|
|
platforms.docker = params;
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-06 05:26:36 +00:00
|
|
|
throw new Error(
|
|
|
|
`Failed to configure platform '${platform}': no endpoint defined`
|
|
|
|
);
|
|
|
|
}
|
2018-08-20 10:53:57 +00:00
|
|
|
config.endpoint = endpoint.replace(/[^/]$/, '$&/');
|
2018-07-06 05:26:36 +00:00
|
|
|
let { host } = config;
|
|
|
|
// extract host from endpoint
|
|
|
|
host = host || (endpoint && URL.parse(endpoint).host);
|
|
|
|
// endpoint is in the format host/path (protocol missing)
|
|
|
|
host = host || (endpoint && URL.parse('http://' + endpoint).host);
|
|
|
|
if (!host) {
|
|
|
|
throw new Error(
|
|
|
|
`Failed to configure platform '${platform}': no host for endpoint '${endpoint}'`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
platforms[platform] = { ...platforms[platform] };
|
2019-02-21 07:01:44 +00:00
|
|
|
logger.debug({ config }, 'Setting hostRule');
|
2018-07-06 05:26:36 +00:00
|
|
|
platforms[platform][host] = { ...platforms[platform][host], ...config };
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
export function find(
|
2019-05-20 04:15:06 +00:00
|
|
|
{
|
|
|
|
platform,
|
|
|
|
host,
|
|
|
|
endpoint,
|
|
|
|
}: { platform: string; host?: string; endpoint?: string },
|
2019-05-18 05:49:53 +00:00
|
|
|
overrides?: IPlatformConfig
|
|
|
|
) {
|
2019-05-20 04:15:06 +00:00
|
|
|
const massagedHost = host
|
|
|
|
? host
|
|
|
|
: endpoint
|
|
|
|
? URL.parse(endpoint).host
|
|
|
|
: undefined;
|
2018-09-13 04:48:08 +00:00
|
|
|
if (!platforms[platform]) {
|
2019-05-20 04:15:06 +00:00
|
|
|
if (massagedHost && hostsOnly[massagedHost]) {
|
|
|
|
return merge(hostsOnly[massagedHost], overrides);
|
2018-10-15 11:37:08 +00:00
|
|
|
}
|
2018-09-13 04:48:08 +00:00
|
|
|
return merge(null, overrides);
|
|
|
|
}
|
2018-09-13 03:31:18 +00:00
|
|
|
// istanbul ignore if
|
|
|
|
if (platform === 'docker') {
|
|
|
|
if (platforms.docker.platform === 'docker') {
|
2018-12-05 17:46:14 +00:00
|
|
|
return merge(platforms.docker, overrides);
|
2018-09-13 03:31:18 +00:00
|
|
|
}
|
2019-05-20 04:15:06 +00:00
|
|
|
return merge(platforms.docker[massagedHost!], overrides);
|
2018-09-13 03:31:18 +00:00
|
|
|
}
|
2019-05-20 04:15:06 +00:00
|
|
|
if (massagedHost) {
|
|
|
|
return merge(platforms[platform][massagedHost], overrides);
|
2018-07-06 05:26:36 +00:00
|
|
|
}
|
|
|
|
const configs = Object.values(platforms[platform]);
|
2019-05-20 08:59:30 +00:00
|
|
|
let config;
|
|
|
|
if (configs.length === 1) {
|
2018-07-06 05:26:36 +00:00
|
|
|
[config] = configs;
|
|
|
|
}
|
|
|
|
return merge(config, overrides);
|
|
|
|
}
|
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
export function hosts({ platform }: { platform: string }) {
|
2018-07-06 05:26:36 +00:00
|
|
|
return Object.keys({ ...platforms[platform] });
|
|
|
|
}
|
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
function merge(config: IPlatformConfig | null, overrides?: IPlatformConfig) {
|
2018-07-06 05:26:36 +00:00
|
|
|
if (!overrides) {
|
|
|
|
return config || null;
|
|
|
|
}
|
|
|
|
const locals = { ...overrides };
|
|
|
|
Object.keys(locals).forEach(key => {
|
|
|
|
if (locals[key] === undefined || locals[key] === null) {
|
|
|
|
delete locals[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return { ...config, ...locals };
|
|
|
|
}
|
|
|
|
|
2019-05-18 05:49:53 +00:00
|
|
|
export function clear() {
|
2018-07-06 05:26:36 +00:00
|
|
|
Object.keys(platforms).forEach(key => delete platforms[key]);
|
|
|
|
}
|