This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·104 lines (89 loc) · 2.51 KB
/
cli.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const _ = require('underscore');
const semver = require('semver');
const cli = require('cli');
const options = cli.parse({
file: ['f', 'A file to process', 'file'],
image: ['i', 'The image name', 'string']
});
const transform = (name, data) => JSON.stringify(imageStream(name, JSON.parse(data)), null, 2);
if (!options.image) {
cli.fatal('You must supply an image name');
}
if (options.file) {
cli.output(transform(options.image, fs.readFileSync(options.file)));
} else {
cli.withStdin(function (data) {
this.output(transform(options.image, data));
});
}
function imageStream (name, releases) {
const current = _.reduce(_.keys(releases), (prev, cur) => Math.max(prev, cur));
const shortName = name.split('/')[1];
if (!shortName) cli.fatal('Image name, with repository prefix is required');
const data = {
kind: 'ImageStream',
apiVersion: 'v1',
metadata: {
name: shortName,
creationTimestamp: `${new Date().toISOString()}`
},
spec: {
tags: []
}
};
_.each(releases, (r) => {
const version = semver.clean(r.version);
const { major } = majorMinor(version);
const versionName = `${major}.x`;
data.spec.tags.push(dockerImageTag(name, versionName));
data.spec.tags.push(imageStreamTag(versionName, versionName));
// if this node version is lts, tag it as such
if (r.lts) {
data.spec.tags.push(imageStreamTag('lts', versionName));
data.spec.tags.push(imageStreamTag(r.lts, versionName));
}
// if this is the current/latest version of node, tag it so
if (major === current) {
data.spec.tags.push(imageStreamTag('latest', versionName));
}
});
return data;
}
function dockerImageTag (name, version) {
return {
name: version,
annotations: annotationsFor(version),
from: {
kind: 'DockerImage',
name: `${name}:${version}`
}
};
}
function imageStreamTag (name, from) {
return {
name,
annotations: annotationsFor(from),
from: {
kind: 'ImageStreamTag',
name: from
}
};
}
function annotationsFor (version) {
return {
description: 'Build and run Node.js applications',
iconClass: 'icon-nodejs',
tags: `builder, node, nodejs, nodejs-${version}`,
supports: `nodejs:${version}, nodejs`,
sampleRepo: 'https://github.com/bucharest-gold/s2i-nodejs.git'
};
}
function majorMinor (version) {
return {
major: semver.major(version),
minor: version.split('.').slice(0, 2).join('.')
};
}