-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
41 lines (33 loc) · 1.14 KB
/
utils.js
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
'use strict';
const safe_split = (text, sep, max) => {
const parts = text.split(sep);
const result = parts.slice(0, max);
if (result.length < max && result.length !== parts.length) {
result.push(parts.slice(max).join(sep));
}
return result;
};
const env_to_obj = (envs) => Object.assign(...envs.map(x => safe_split(x, '=', 2)).map(([key, value]) => ({ [key]: value })));
const extract_tag = (config, info) => {
const tag = {};
const container_labels = info.ContainerLabels || {};
if (config.labels) {
config.labels.split(',').filter(x => container_labels[x]).forEach(name => {
tag[name] = container_labels[name];
});
}
if (config.env && info.ContainerEnv) {
const env = env_to_obj(info.ContainerEnv);
config.env.split(',').filter(x => env[x]).forEach(name => {
tag[name] = env[name];
});
}
if (config['env-regexp'] && info.ContainerEnv) {
const r = new RegExp(config['env-regexp']);
Object.assign(tag, env_to_obj(info.ContainerEnv.filter(x => r.test(x))));
}
return tag;
};
module.exports = {
extract_tag,
};