35 lines
1019 B
JavaScript
35 lines
1019 B
JavaScript
import fs from 'fs'
|
|
|
|
const DEFAULT_SECRETS_PATH = '/etc/p42/secrets.env'
|
|
|
|
function stripQuotes(value) {
|
|
if(
|
|
(value.startsWith('"') && value.endsWith('"')) ||
|
|
(value.startsWith("'") && value.endsWith("'"))
|
|
) {
|
|
return(value.slice(1, -1))
|
|
}
|
|
return(value)
|
|
}
|
|
|
|
export function loadP42Secrets(filePath = DEFAULT_SECRETS_PATH) {
|
|
if(process.env.mysql_user && process.env.mysql_pass) return(true)
|
|
|
|
if(!fs.existsSync(filePath)) return(false)
|
|
|
|
const text = fs.readFileSync(filePath, 'utf8')
|
|
for(const rawLine of text.split('\n')) {
|
|
const line = rawLine.trim()
|
|
if(!line || line.startsWith('#')) continue
|
|
const eq = line.indexOf('=')
|
|
if(eq < 1) continue
|
|
const key = line.slice(0, eq).trim()
|
|
const value = stripQuotes(line.slice(eq + 1).trim())
|
|
if(key === 'mysql_user' || key === 'mysql_pass') {
|
|
process.env[key] = value
|
|
}
|
|
}
|
|
|
|
return(Boolean(process.env.mysql_user && process.env.mysql_pass))
|
|
}
|