@@ -3,12 +3,11 @@ import Octokit, {
3
3
} from '@octokit/rest'
4
4
import { spawnSync , SpawnSyncReturns } from 'child_process'
5
5
import fs from 'fs'
6
- import fetch from 'node-fetch'
6
+ import fetch , { Response } from 'node-fetch'
7
7
import zlib from 'zlib'
8
8
import tar from 'tar'
9
9
10
10
import { TrivyOption , Vulnerability } from './interface'
11
- import { workerData } from 'worker_threads'
12
11
13
12
interface Repository {
14
13
owner : string ,
@@ -30,30 +29,12 @@ export class Downloader {
30
29
public async download ( version : string ) : Promise < string > {
31
30
const os : string = this . checkPlatform ( process . platform )
32
31
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
57
38
}
58
39
59
40
private checkPlatform ( platform : string ) : string {
@@ -104,7 +85,22 @@ export class Downloader {
104
85
` )
105
86
}
106
87
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 {
108
104
const trivyCmdPaths : string [ ] = fs . readdirSync ( baseDir ) . filter ( f => f === 'trivy' )
109
105
console . log ( trivyCmdPaths )
110
106
return trivyCmdPaths . length === 1
0 commit comments