1
+ //@ts -nocheck
2
+ // @ts -ignore
3
+ import blessed from 'blessed'
4
+ // @ts -ignore
5
+ import contrib from 'blessed-contrib'
1
6
import meow from 'meow'
2
7
import ora from 'ora'
3
8
@@ -114,6 +119,17 @@ function setupCommand(
114
119
}
115
120
}
116
121
122
+ type ThreatResult = {
123
+ createdAt : string
124
+ description : string
125
+ id : number ,
126
+ locationHtmlUrl : string
127
+ packageHtmlUrl : string
128
+ purl : string
129
+ removedAt : string
130
+ threatType : string
131
+ }
132
+
117
133
async function fetchThreatFeed (
118
134
{ per_page, page, direction, filter } : CommandContext ,
119
135
spinner : Ora ,
@@ -122,10 +138,48 @@ async function fetchThreatFeed(
122
138
const formattedQueryParams = formatQueryParams ( { per_page, page, direction, filter } ) . join ( '&' )
123
139
124
140
const response = await queryAPI ( `threat-feed?${ formattedQueryParams } ` , apiKey )
125
- const data = await response . json ( ) ;
141
+ const data : { results : ThreatResult [ ] , nextPage : string } = await response . json ( ) ;
126
142
127
143
spinner . stop ( )
128
- console . log ( data )
144
+
145
+ const screen = blessed . screen ( )
146
+
147
+ var table = contrib . table (
148
+ { keys : 'true'
149
+ , fg : 'white'
150
+ , selectedFg : 'white'
151
+ , selectedBg : 'magenta'
152
+ , interactive : 'true'
153
+ , label : 'Threat feed'
154
+ , width : '100%'
155
+ , height : '100%'
156
+ , border : { type : "line" , fg : "cyan" }
157
+ , columnSpacing : 10 //in chars
158
+ , columnWidth : [ 20 , 20 , 20 , 20 , 20 ] /*in chars*/ } )
159
+
160
+ //allow control the table with the keyboard
161
+ table . focus ( )
162
+
163
+ screen . append ( table )
164
+
165
+ const formattedOutput = formatResults ( data . results )
166
+
167
+ table . setData (
168
+ { headers : [ 'Ecosystem' , 'Threat type' , 'Name' , 'Version' , 'Detected at' ] , data : formattedOutput } )
169
+
170
+ screen . render ( )
171
+
172
+ screen . key ( [ 'escape' , 'q' , 'C-c' ] , ( ) => process . exit ( 0 ) )
173
+ }
174
+
175
+ const formatResults = ( data : ThreatResult [ ] ) => {
176
+ return data . map ( d => {
177
+ const ecosystem = d . purl . split ( 'pkg:' ) [ 1 ] . split ( '/' ) [ 0 ]
178
+ const name = d . purl . split ( '/' ) [ 1 ] . split ( '@' ) [ 0 ]
179
+ const version = d . purl . split ( '@' ) [ 1 ]
180
+
181
+ return [ ecosystem , d . threatType , name , version , d . createdAt ]
182
+ } )
129
183
}
130
184
131
185
const formatQueryParams = ( params : any ) => {
0 commit comments