@@ -35,6 +35,8 @@ import { PupRestClient } from "@pup/api-client"
35
35
import { CurrentRuntime , Runtime } from "@cross/runtime"
36
36
import { Prop } from "../common/prop.ts"
37
37
import { encodeBase64 } from "@std/encoding/base64"
38
+ import { ApiLogItem } from "@pup/api-definitions"
39
+ import { EventHandler } from "@pup/common/eventemitter"
38
40
39
41
/**
40
42
* Define the main entry point of the CLI application
@@ -222,7 +224,7 @@ async function main() {
222
224
223
225
// Send api request
224
226
const apiBaseUrl = `http://${ configuration . api ?. hostname || DEFAULT_REST_API_HOSTNAME } :${ port } `
225
- client = new PupRestClient ( apiBaseUrl , token ! )
227
+ client = new PupRestClient ( apiBaseUrl , token ! , baseArgument === "monitor" , 3000 , 1 )
226
228
} catch ( _e ) {
227
229
/* Ignore */
228
230
}
@@ -240,6 +242,9 @@ async function main() {
240
242
const expiresInSeconds = checkedArgs . get ( "expire-in" )
241
243
if ( expiresInSeconds ) {
242
244
expiresAt = Date . now ( ) + ( parseInt ( expiresInSeconds , 10 ) * 1000 )
245
+ } else {
246
+ console . error ( "Error: You need to specify expiry time using --expire-in <seconds>" )
247
+ exit ( 1 )
243
248
}
244
249
const token = await GenerateToken (
245
250
secret ,
@@ -360,59 +365,64 @@ async function main() {
360
365
* using websockets, and prints all received messages
361
366
*/
362
367
if ( baseArgument === "monitor" ) {
363
- const apiHostname = configuration . api ?. hostname || DEFAULT_REST_API_HOSTNAME
364
- const apiPort = port
365
- const wsUrl = `ws://${ apiHostname } :${ apiPort } /wss`
366
- const wss = new WebSocketStream ( wsUrl , {
367
- headers : {
368
- "Authorization" : `Bearer ${ token } ` ,
369
- } ,
370
- } )
371
- const { readable } = await wss . opened
372
- const reader = readable . getReader ( )
373
- while ( true ) {
374
- const { value, done } = await reader . read ( )
375
- if ( done ) {
376
- break
377
- }
368
+ const logHandler = ( e : ApiLogItem ) => {
378
369
try {
379
- const v = JSON . parse ( value . toString ( ) )
380
- if ( v . t === "log" ) {
381
- const logWithColors = configuration ! . logger ?. colors ?? true
382
-
383
- const { processId, severity, category, timeStamp, text } = v . d
384
-
385
- const severityFilter = ! checkedArgs . get ( "severity" ) || checkedArgs . get ( "severity" ) === "" || checkedArgs . get ( "severity" ) ! . toLowerCase ( ) === severity . toLowerCase ( )
386
- const processFilter = ! checkedArgs . get ( "id" ) || checkedArgs . get ( "id" ) === "" || ! processId || checkedArgs . get ( "id" ) ! . toLowerCase ( ) === processId . toLowerCase ( )
387
-
388
- if ( ! severityFilter ) continue
389
- if ( ! processFilter ) continue
390
-
391
- const isStdErr = severity === "error" || category === "stderr"
392
- const decoratedLogText = `${ new Date ( timeStamp ) . toISOString ( ) } [${ severity . toUpperCase ( ) } ] [${ processId || "core" } :${ category } ] ${ text } `
393
- let color = null
394
- // Apply coloring rules
395
- if ( logWithColors ) {
396
- if ( processId === "core" ) color = "gray"
397
- if ( category === "starting" ) color = "green"
398
- if ( category === "finished" ) color = "yellow"
399
- if ( isStdErr ) color = "red"
400
- }
401
- let logFn = console . log
402
- if ( severity === "warn" ) logFn = console . warn
403
- if ( severity === "info" ) logFn = console . info
404
- if ( severity === "error" ) logFn = console . error
405
- if ( color !== null ) {
406
- logFn ( `%c${ decoratedLogText } ` , `color: ${ color } ` )
407
- } else {
408
- logFn ( decoratedLogText )
409
- }
370
+ const logWithColors = configuration ! . logger ?. colors ?? true
371
+ const { processId, severity, category, timeStamp, text } = e
372
+ const severityFilter = ! checkedArgs . get ( "severity" ) || checkedArgs . get ( "severity" ) === "" || checkedArgs . get ( "severity" ) ! . toLowerCase ( ) === severity . toLowerCase ( )
373
+ const processFilter = ! checkedArgs . get ( "id" ) || checkedArgs . get ( "id" ) === "" || ! processId || checkedArgs . get ( "id" ) ! . toLowerCase ( ) === processId . toLowerCase ( )
374
+
375
+ if ( ! severityFilter ) return
376
+ if ( ! processFilter ) return
377
+
378
+ const isStdErr = severity === "error" || category === "stderr"
379
+ const decoratedLogText = `${ new Date ( timeStamp ) . toISOString ( ) } [${ severity . toUpperCase ( ) } ] [${ processId || "core" } :${ category } ] ${ text } `
380
+ let color = null
381
+ // Apply coloring rules
382
+ if ( logWithColors ) {
383
+ if ( processId === "core" ) color = "gray"
384
+ if ( category === "starting" ) color = "green"
385
+ if ( category === "finished" ) color = "yellow"
386
+ if ( isStdErr ) color = "red"
387
+ }
388
+ let logFn = console . log
389
+ if ( severity === "warn" ) logFn = console . warn
390
+ if ( severity === "info" ) logFn = console . info
391
+ if ( severity === "error" ) logFn = console . error
392
+ if ( color !== null ) {
393
+ logFn ( `%c${ decoratedLogText } ` , `color: ${ color } ` )
394
+ } else {
395
+ logFn ( decoratedLogText )
410
396
}
411
397
} catch ( _e ) {
412
398
console . error ( "Error in log streamer: " + _e )
413
399
}
414
400
}
415
- return
401
+
402
+ // Test the client
403
+ let responseState
404
+ try {
405
+ responseState = await client ?. getState ( )
406
+ if ( ! responseState ?. data ) {
407
+ console . error ( "Could not contact the Pup instance." )
408
+ exit ( 1 )
409
+ }
410
+ } catch ( _e ) {
411
+ console . error ( "Action failed: Could not contact the Pup instance." )
412
+ exit ( 1 )
413
+ }
414
+
415
+ // Output status
416
+ console . log ( `Connected to Pup, streaming logs. Abort with CTRL+C.` )
417
+
418
+ // Start streaming logs
419
+ client ?. on ( "log" , logHandler as EventHandler < unknown > )
420
+
421
+ // Wait a year or so
422
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 365 * 24 * 60 * 60 * 1000 ) )
423
+
424
+ // Exit
425
+ exit ( 0 )
416
426
}
417
427
418
428
/**
@@ -537,7 +547,7 @@ async function main() {
537
547
return exit ( 1 )
538
548
}
539
549
} catch ( e ) {
540
- console . error ( "Action failed:" , e )
550
+ console . error ( "Action failed:" , e . name )
541
551
return exit ( 1 )
542
552
}
543
553
}
@@ -556,7 +566,11 @@ async function main() {
556
566
console . warn ( `Pup already running. Exiting.` )
557
567
exit ( 1 )
558
568
}
559
- } catch ( _e ) { /* Expected! ^*/ }
569
+ } catch ( _e ) {
570
+ /* Expected! ^*/
571
+ } finally {
572
+ client ?. close ( )
573
+ }
560
574
561
575
/**
562
576
* Error handling: Require at least one configured process
0 commit comments