Skip to content

Commit c482731

Browse files
committed
[trivy.ts] Add saveTrivyCmd method to save downloaded file
1 parent b1c0a80 commit c482731

File tree

2 files changed

+40
-46
lines changed

2 files changed

+40
-46
lines changed

dist/index.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13215,26 +13215,12 @@ class Downloader {
1321513215
return __awaiter(this, void 0, void 0, function* () {
1321613216
const os = this.checkPlatform(process.platform);
1321713217
const downloadUrl = yield this.getDownloadUrl(version, os);
13218-
console.log(downloadUrl);
13218+
console.debug(downloadUrl);
1321913219
const response = yield node_fetch_1.default(downloadUrl);
13220-
const workspace = process.env.GTIHUB_WORKSPACE || '.';
13221-
response.body.pipe(zlib_1.default.createGunzip()).pipe(tar_1.default.extract({ path: workspace }));
13222-
// let result = spawnSync(
13223-
// 'curl',
13224-
// ['-Lo', trivyCompressedPath, downloadUrl],
13225-
// { encoding: 'utf-8' }
13226-
// )
13227-
// if (result.error) throw result.error
13228-
// result = spawnSync(
13229-
// 'tar',
13230-
// ['xzf', trivyCompressedPath],
13231-
// { encoding: 'utf-8' }
13232-
// )
13233-
// if (result.error) throw result.error
13234-
if (!this.trivyExists(workspace)) {
13235-
throw new Error('Failed to extract Trivy command file.');
13236-
}
13237-
return `${workspace}/trivy`;
13220+
const trivyCmdBaseDir = process.env.GITHUB_WORKSPACE || '.';
13221+
const trivyCmdPath = yield this.saveTrivyCmd(response, trivyCmdBaseDir);
13222+
console.debug(trivyCmdPath);
13223+
return trivyCmdPath;
1323813224
});
1323913225
}
1324013226
checkPlatform(platform) {
@@ -13291,6 +13277,18 @@ class Downloader {
1329113277
`);
1329213278
});
1329313279
}
13280+
saveTrivyCmd(response, savedPath = '.') {
13281+
return new Promise((resolve, reject) => {
13282+
const extract = tar_1.default.extract({ path: savedPath });
13283+
response.body.pipe(zlib_1.default.createGunzip()).pipe(extract);
13284+
extract.on('finish', () => {
13285+
if (!this.trivyExists(savedPath)) {
13286+
reject('Failed to extract Trivy command file.');
13287+
}
13288+
resolve(`${savedPath}/trivy`);
13289+
});
13290+
});
13291+
}
1329413292
trivyExists(baseDir) {
1329513293
const trivyCmdPaths = fs_1.default.readdirSync(baseDir).filter(f => f === 'trivy');
1329613294
console.log(trivyCmdPaths);

src/trivy.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import Octokit, {
33
} from '@octokit/rest'
44
import { spawnSync, SpawnSyncReturns } from 'child_process'
55
import fs from 'fs'
6-
import fetch from 'node-fetch'
6+
import fetch, { Response } from 'node-fetch'
77
import zlib from 'zlib'
88
import tar from 'tar'
99

1010
import { TrivyOption, Vulnerability } from './interface'
11-
import { workerData } from 'worker_threads'
1211

1312
interface Repository {
1413
owner: string,
@@ -30,30 +29,12 @@ export class Downloader {
3029
public async download(version: string): Promise<string> {
3130
const os: string = this.checkPlatform(process.platform)
3231
const downloadUrl: string = await this.getDownloadUrl(version, os)
33-
console.log(downloadUrl)
34-
const response = await fetch(downloadUrl)
35-
const workspace: string = process.env.GTIHUB_WORKSPACE || '.'
36-
response.body.pipe(zlib.createGunzip()).pipe(tar.extract({ path: workspace }))
37-
38-
// let result = spawnSync(
39-
// 'curl',
40-
// ['-Lo', trivyCompressedPath, downloadUrl],
41-
// { encoding: 'utf-8' }
42-
// )
43-
// if (result.error) throw result.error
44-
45-
// result = spawnSync(
46-
// 'tar',
47-
// ['xzf', trivyCompressedPath],
48-
// { encoding: 'utf-8' }
49-
// )
50-
// if (result.error) throw result.error
51-
52-
if (!this.trivyExists(workspace)) {
53-
throw new Error('Failed to extract Trivy command file.')
54-
}
55-
56-
return `${workspace}/trivy`
32+
console.debug(downloadUrl)
33+
const response: Response = await fetch(downloadUrl)
34+
const trivyCmdBaseDir: string = process.env.GITHUB_WORKSPACE || '.'
35+
const trivyCmdPath: string = await this.saveTrivyCmd(response, trivyCmdBaseDir)
36+
console.debug(trivyCmdPath)
37+
return trivyCmdPath
5738
}
5839

5940
private checkPlatform(platform: string): string {
@@ -104,7 +85,22 @@ export class Downloader {
10485
`)
10586
}
10687

107-
trivyExists(baseDir: string): boolean {
88+
private saveTrivyCmd(response: Response, savedPath: string = '.'): Promise<string> {
89+
return new Promise((resolve, reject) => {
90+
const extract = tar.extract({ path: savedPath })
91+
response.body.pipe(zlib.createGunzip()).pipe(extract)
92+
93+
extract.on('finish', () => {
94+
if (!this.trivyExists(savedPath)) {
95+
reject('Failed to extract Trivy command file.')
96+
}
97+
resolve(`${savedPath}/trivy`)
98+
})
99+
})
100+
101+
}
102+
103+
public trivyExists(baseDir: string): boolean {
108104
const trivyCmdPaths: string[] = fs.readdirSync(baseDir).filter(f => f === 'trivy')
109105
console.log(trivyCmdPaths)
110106
return trivyCmdPaths.length === 1

0 commit comments

Comments
 (0)