-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (22 loc) · 979 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
const fs = require('fs');
const path = require('path');
module.exports = function read(secretName) {
if(process.env.NODE_ENV !== 'production'){
console.log(`Loading ${secretName} from ${path.join('.', '.env')}`);
const iniData = fs.readFileSync(path.join('.', '.env'), 'utf8');
const vars = Object.fromEntries(iniData.split('\n').map(varValue => varValue.split('=')));
if(!vars[secretName]){
throw new Error(`An error occurred while trying to read the secret ${secretName} from .env. Are you sure it's defined?`);
}
return vars[secretName];
}
console.log(`Loading ${secretName} from /run/secrets/${secretName}`);
try {
return fs.readFileSync(`/run/secrets/${secretName}`, 'utf8');
} catch(err) {
if (err.code !== 'ENOENT') {
throw new Error(`An error occurred while trying to read the secret: ${secretName}. Err: ${err}`);
}
return false;
}
};