renovate/lib/util/host-rules.ts

113 lines
2.9 KiB
TypeScript
Raw Normal View History

import URL from 'url';
2018-07-06 05:26:36 +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
export function update(params: IPlatformConfig) {
2018-07-06 05:26:36 +00:00
const { platform } = params;
if (!platform) {
if (params.endpoint) {
const { host } = URL.parse(params.endpoint);
hostsOnly[host!] = params;
return true;
}
throw new Error(
'Failed to set configuration: no platform or endpoint specified'
);
2018-07-06 05:26:36 +00:00
}
const config = { ...params };
2018-07-06 05:26:36 +00:00
const { endpoint } = config;
if (!endpoint) {
// 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`
);
}
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;
}
export function find(
{
platform,
host,
endpoint,
}: { platform: string; host?: string; endpoint?: string },
overrides?: IPlatformConfig
) {
const massagedHost = host
? host
: endpoint
? URL.parse(endpoint).host
: undefined;
2018-09-13 04:48:08 +00:00
if (!platforms[platform]) {
if (massagedHost && hostsOnly[massagedHost]) {
return merge(hostsOnly[massagedHost], overrides);
}
2018-09-13 04:48:08 +00:00
return merge(null, overrides);
}
// istanbul ignore if
if (platform === 'docker') {
if (platforms.docker.platform === 'docker') {
return merge(platforms.docker, overrides);
}
return merge(platforms.docker[massagedHost!], overrides);
}
if (massagedHost) {
return merge(platforms[platform][massagedHost], overrides);
2018-07-06 05:26:36 +00:00
}
const configs = Object.values(platforms[platform]);
let config;
if (configs.length === 1) {
2018-07-06 05:26:36 +00:00
[config] = configs;
}
return merge(config, overrides);
}
export function hosts({ platform }: { platform: string }) {
2018-07-06 05:26:36 +00:00
return Object.keys({ ...platforms[platform] });
}
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 };
}
export function clear() {
2018-07-06 05:26:36 +00:00
Object.keys(platforms).forEach(key => delete platforms[key]);
}