-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot.mjs
65 lines (61 loc) · 1.75 KB
/
dot.mjs
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
import { readFileSync } from "fs";
function skbomDot(data) {
return `
digraph cluster{
rankdir="LR";
label="${data.name}:${data.version}";
node[shape="box";style="filled,solid";color=mediumpurple;fillcolor=bisque];
subgraph masters {
cluster=true;
label="Masters";
style="solid";
${data.controlPlane.nodes.reduce((acc, n) => acc + printNode(n) + "\n", "")}
}
subgraph controlplane {
cluster=true;
label="Control Plane";
style="solid";
${data.controlPlane.components.reduce((acc, r) => acc + printResource(r) + "\n", "")}
}
subgraph nodes {
cluster=true;
label="Workers";
style="solid";
${data.nodes.reduce((acc, n) => acc + printNode(n) + "\n", "")}
}
subgraph components {
cluster=true;
label="Componenets";
style="solid";
${data.components.reduce((acc, r) => acc+ printResource(r) + "\n", "")}
}
}`;
}
function printNode(n) {
return `subgraph "${n.id}" {
cluster=true;
label="${n.name}";
style="filled,solid";
color=olive;
fillcolor=lightyellow;
${n.resources.reduce((acc, r) => acc + " " + printResource(r) + "\n", "")}
${n.id}_os[label="${n.os.name}:${n.os.version}"; style="filled,solid"; color="mediumpurple"; fillcolor="LightSteelBlue"; shape="hexagon"]
}`;
}
function printResource(r) {
let res = `${r.id}[label="${r.name}:${r.version}"]`;
res += r.resources.reduce((acc, v) => acc+`\n${v.id}[label="${v.name}:${v.version}"]\n${r.id} -> ${v.id}`, "");
return res;
}
(() => {
const filePath = process.argv[2];
let data;
try {
const text = readFileSync(filePath, 'utf8');
data = JSON.parse(text);
} catch (err) {
console.error(err);
}
let mmd = skbomDot(data);
console.log(mmd);
})();