1- const { getServerAddress } = require ( './helpers' ) ;
1+ /* eslint-disable promise/catch-or-return */
2+
3+ // ↓ Should be imported first
4+ const { terminate } = require ( '@codefresh-io/cf-telemetry/init' ) ;
5+ // ↓ Keep one blank line below to prevent automatic import reordering
6+
7+ const { Logger } = require ( '@codefresh-io/cf-telemetry/logs' ) ;
8+ const { getServerAddress, registerExitHandlers } = require ( './helpers' ) ;
9+
10+ const logger = new Logger ( 'codefresh:containerLogger:addNewMask' ) ;
211
312const exitCodes = {
413 success : 0 ,
@@ -14,7 +23,7 @@ const exitCodes = {
1423let exitWithError = true ;
1524const exitHandler = ( exitCode ) => {
1625 if ( ( ! exitCode || ! process . exitCode ) && exitWithError ) {
17- console . warn ( `Unexpected exit with code 0. Exiting with ${ exitCodes . unexpectedSuccess } instead` ) ;
26+ logger . warn ( `Unexpected exit with code 0. Exiting with ${ exitCodes . unexpectedSuccess } instead` ) ;
1827 process . exitCode = exitCodes . unexpectedSuccess ;
1928 }
2029} ;
@@ -23,7 +32,7 @@ process.on('exit', exitHandler);
2332async function updateMasks ( secret ) {
2433 try {
2534 const serverAddress = await getServerAddress ( ) ;
26- console . debug ( `server address: ${ serverAddress } ` ) ;
35+ logger . debug ( `server address: ${ serverAddress } ` ) ;
2736 const url = new URL ( 'secrets' , serverAddress ) ;
2837
2938 // eslint-disable-next-line import/no-unresolved
@@ -34,24 +43,26 @@ async function updateMasks(secret) {
3443 } ) ;
3544
3645 if ( response . statusCode === 201 ) {
37- console . log ( `successfully updated masks with secret: ${ secret . key } ` ) ;
46+ logger . log ( `successfully updated masks with secret: ${ secret . key } ` ) ;
3847 exitWithError = false ;
39- process . exit ( exitCodes . success ) ;
48+ terminate ( ) . finally ( ( ) => process . exit ( exitCodes . success ) ) ;
4049 } else {
41- console . error ( `could not create mask for secret: ${ secret . key } . Server responded with: ${ response . statusCode } \n\n${ response . body } ` ) ;
42- process . exit ( exitCodes . error ) ;
50+ logger . error ( `could not create mask for secret: ${ secret . key } . Server responded with: ${ response . statusCode } \n\n${ response . body } ` ) ;
51+ terminate ( ) . finally ( ( ) => process . exit ( exitCodes . error ) ) ;
4352 }
4453 } catch ( error ) {
45- console . error ( `could not create mask for secret: ${ secret . key } . Error: ${ error } ` ) ;
46- process . exit ( exitCodes . error ) ;
54+ logger . error ( `could not create mask for secret: ${ secret . key } . Error: ${ error } ` ) ;
55+ terminate ( ) . finally ( ( ) => process . exit ( exitCodes . error ) ) ;
4756 }
4857}
4958
5059if ( require . main === module ) {
60+ registerExitHandlers ( ) ;
61+
5162 // first argument is the secret key second argument is the secret value
5263 if ( process . argv . length < 4 ) {
53- console . log ( 'not enough arguments, need secret key and secret value' ) ;
54- process . exit ( exitCodes . missingArguments ) ;
64+ logger . log ( 'not enough arguments, need secret key and secret value' ) ;
65+ terminate ( ) . finally ( ( ) => process . exit ( exitCodes . missingArguments ) ) ;
5566 }
5667 const key = process . argv [ 2 ] ;
5768 const value = process . argv [ 3 ] ;
0 commit comments