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' ) ;
2
11
3
12
const exitCodes = {
4
13
success : 0 ,
@@ -14,7 +23,7 @@ const exitCodes = {
14
23
let exitWithError = true ;
15
24
const exitHandler = ( exitCode ) => {
16
25
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` ) ;
18
27
process . exitCode = exitCodes . unexpectedSuccess ;
19
28
}
20
29
} ;
@@ -23,7 +32,7 @@ process.on('exit', exitHandler);
23
32
async function updateMasks ( secret ) {
24
33
try {
25
34
const serverAddress = await getServerAddress ( ) ;
26
- console . debug ( `server address: ${ serverAddress } ` ) ;
35
+ logger . debug ( `server address: ${ serverAddress } ` ) ;
27
36
const url = new URL ( 'secrets' , serverAddress ) ;
28
37
29
38
// eslint-disable-next-line import/no-unresolved
@@ -34,24 +43,26 @@ async function updateMasks(secret) {
34
43
} ) ;
35
44
36
45
if ( response . statusCode === 201 ) {
37
- console . log ( `successfully updated masks with secret: ${ secret . key } ` ) ;
46
+ logger . log ( `successfully updated masks with secret: ${ secret . key } ` ) ;
38
47
exitWithError = false ;
39
- process . exit ( exitCodes . success ) ;
48
+ terminate ( ) . finally ( ( ) => process . exit ( exitCodes . success ) ) ;
40
49
} 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 ) ) ;
43
52
}
44
53
} 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 ) ) ;
47
56
}
48
57
}
49
58
50
59
if ( require . main === module ) {
60
+ registerExitHandlers ( ) ;
61
+
51
62
// first argument is the secret key second argument is the secret value
52
63
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 ) ) ;
55
66
}
56
67
const key = process . argv [ 2 ] ;
57
68
const value = process . argv [ 3 ] ;
0 commit comments