-
Notifications
You must be signed in to change notification settings - Fork 9
/
in.ts
97 lines (76 loc) · 4.09 KB
/
in.ts
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
#!/usr/bin/env node
import * as fs from "fs";
import * as path from "path";
import * as util from "util";
import fetch from "node-fetch";
import { retrieveRequestFromStdin, createFetchHeaders } from "./index";
import { InRequest, InResponse } from "./index";
// Promisified funtions from the Node.js standard library.
const writeFile = util.promisify(fs.writeFile);
(async () => {
// Determine destination path.
if (process.argv.length != 3) {
process.stderr.write(`Expected exactly one argument (destination), got ${process.argv.length - 2}.`);
process.exit(2);
}
const destination = path.resolve(process.argv[2]);
const request = await retrieveRequestFromStdin<InRequest>();
const headers = createFetchHeaders(request);
// Fetch metadata
const chartResp = await fetch(`${request.source.server_url}/${request.source.chart_name}/${request.version.version}`, { headers: headers });
const chartJson = await chartResp.json();
if (request.source.harbor_api === true) {
// Read params and pre-initialize them with documented default values.
let targetBasename: string = `${chartJson.metadata.name}-${chartJson.metadata.version}`;
if (request.params != null) {
if (request.params.target_basename != null) {
targetBasename = request.params.target_basename;
}
}
const response: InResponse = {
version: {
version: chartJson.metadata.version,
digest: chartJson.metadata.digest
},
metadata: [
{ name: "created", value: chartJson.metadata.created },
{ name: "description", value: chartJson.metadata.description }
]
}
var non_api_server_url = request.source.server_url.replace("api/","")
const tgzResp = await fetch(`${non_api_server_url}/${request.source.chart_name}-${chartJson.metadata.version}.tgz`, { headers: headers });
await writeFile(path.resolve(destination, `${targetBasename}.tgz`), await tgzResp.buffer());
const provResp = await fetch(`${non_api_server_url}/${request.source.chart_name}-${chartJson.metadata.version}.tgz.prov`, { headers: headers });
await writeFile(path.resolve(destination, `${targetBasename}.tgz.prov`), await provResp.buffer());
await writeFile(path.resolve(destination, `${targetBasename}.json`), JSON.stringify(chartJson));
process.stdout.write(JSON.stringify(response, null, 2));
} else {
// Read params and pre-initialize them with documented default values.
let targetBasename: string = `${chartJson.name}-${chartJson.version}`;
if (request.params != null) {
if (request.params.target_basename != null) {
targetBasename = request.params.target_basename;
}
}
const response: InResponse = {
version: {
version: chartJson.version,
digest: chartJson.digest
},
metadata: [
{ name: "created", value: chartJson.created },
{ name: "description", value: chartJson.description },
{ name: "appVersion", value: chartJson.appVersion },
{ name: "home", value: chartJson.home },
{ name: "tillerVersion", value: chartJson.tillerVersion }
]
}
var non_api_server_url = request.source.server_url.replace("api/","")
const tgzResp = await fetch(`${non_api_server_url}/${request.source.chart_name}-${chartJson.version}.tgz`, { headers: headers });
await writeFile(path.resolve(destination, `${targetBasename}.tgz`), await tgzResp.buffer());
const provResp = await fetch(`${non_api_server_url}/${request.source.chart_name}-${chartJson.version}.tgz.prov`, { headers: headers });
await writeFile(path.resolve(destination, `${targetBasename}.tgz.prov`), await provResp.buffer());
await writeFile(path.resolve(destination, `${targetBasename}.json`), JSON.stringify(chartJson));
process.stdout.write(JSON.stringify(response, null, 2));
}
})();