diff --git a/src/install.js b/src/install.js index 89b40d7b39..4d8904820c 100644 --- a/src/install.js +++ b/src/install.js @@ -47,10 +47,25 @@ questions.optional = [ }, ]; +// gpt assisted code function checkSetupFlagEnv() { let setupVal = install.values; + const envConfMap = getEnvConfMap(); + if (hasRelevantEnvVars(envConfMap)) { + winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...'); + setupVal = setUpValuesFromEnv(setupVal, envConfMap); + } + setupVal = getSetupValuesFromJson(setupVal); + + if (setupVal && typeof setupVal === 'object') { + validateSetupValues(setupVal); + } else if (nconf.get('database')) { + setDatabaseValues(); + } +} - const envConfMap = { +function getEnvConfMap() { + return { CONFIG: 'config', NODEBB_CONFIG: 'config', NODEBB_URL: 'url', @@ -66,90 +81,105 @@ function checkSetupFlagEnv() { NODEBB_DB_NAME: 'database', NODEBB_DB_SSL: 'ssl', }; +} - // Set setup values from env vars (if set) +function hasRelevantEnvVars(envConfMap) { const envKeys = Object.keys(process.env); - if (Object.keys(envConfMap).some(key => envKeys.includes(key))) { - winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...'); - setupVal = setupVal || {}; - - Object.entries(process.env).forEach(([evName, evValue]) => { // get setup values from env - if (evName.startsWith('NODEBB_DB_')) { - setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue; - } else if (evName.startsWith('NODEBB_')) { - setupVal[envConfMap[evName]] = evValue; - } - }); + return Object.keys(envConfMap).some(key => envKeys.includes(key)); +} - setupVal['admin:password:confirm'] = setupVal['admin:password']; - } +function setUpValuesFromEnv(setupVal, envConfMap) { + setupVal = setupVal || {}; + Object.entries(process.env).forEach(([evName, evValue]) => { + if (evName.startsWith('NODEBB_DB_')) { + setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue; + } else if (evName.startsWith('NODEBB_')) { + setupVal[envConfMap[evName]] = evValue; + } + }); + setupVal['admin:password:confirm'] = setupVal['admin:password']; + return setupVal; +} - // try to get setup values from json, if successful this overwrites all values set by env - // TODO: better behaviour would be to support overrides per value, i.e. in order of priority (generic pattern): - // flag, env, config file, default +function getSetupValuesFromJson(setupVal) { try { if (nconf.get('setup')) { const setupJSON = JSON.parse(nconf.get('setup')); - setupVal = { ...setupVal, ...setupJSON }; + return { ...setupVal, ...setupJSON }; } } catch (err) { winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json'); } + return setupVal; +} - if (setupVal && typeof setupVal === 'object') { - if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) { - install.values = setupVal; - } else { - winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:'); - if (!setupVal['admin:username']) { - winston.error(' admin:username'); - } - if (!setupVal['admin:password']) { - winston.error(' admin:password'); - } - if (!setupVal['admin:password:confirm']) { - winston.error(' admin:password:confirm'); - } - if (!setupVal['admin:email']) { - winston.error(' admin:email'); - } - - process.exit(); - } - } else if (nconf.get('database')) { - install.values = install.values || {}; - install.values.database = nconf.get('database'); +function validateSetupValues(setupVal) { + if (setupVal['admin:username'] && setupVal['admin:password'] && setupVal['admin:password:confirm'] && setupVal['admin:email']) { + install.values = setupVal; + } else { + winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:'); + logMissingSetupValues(setupVal); + process.exit(); } } +function logMissingSetupValues(setupVal) { + if (!setupVal['admin:username']) winston.error(' admin:username'); + if (!setupVal['admin:password']) winston.error(' admin:password'); + if (!setupVal['admin:password:confirm']) winston.error(' admin:password:confirm'); + if (!setupVal['admin:email']) winston.error(' admin:email'); +} + +function setDatabaseValues() { + install.values = install.values || {}; + install.values.database = nconf.get('database'); +} + +// GPT assisted code function checkCIFlag() { - let ciVals; + const ciVals = getCIVals(); + + if (isValidCIVals(ciVals)) { + install.ciVals = ciVals; + } else { + handleMissingCIValues(ciVals); + } +} + +function getCIVals() { try { - ciVals = JSON.parse(nconf.get('ci')); + return JSON.parse(nconf.get('ci')); } catch (e) { - ciVals = undefined; + return undefined; + } +} + +function isValidCIVals(ciVals) { + return ciVals && ciVals instanceof Object && + ciVals.hasOwnProperty('host') && + ciVals.hasOwnProperty('port') && + ciVals.hasOwnProperty('database'); +} + +function handleMissingCIValues(ciVals) { + winston.error('[install/checkCIFlag] required values are missing for automated CI integration:'); + + if (!ciVals.hasOwnProperty('host')) { + winston.error(' host'); } - if (ciVals && ciVals instanceof Object) { - if (ciVals.hasOwnProperty('host') && ciVals.hasOwnProperty('port') && ciVals.hasOwnProperty('database')) { - install.ciVals = ciVals; - } else { - winston.error('[install/checkCIFlag] required values are missing for automated CI integration:'); - if (!ciVals.hasOwnProperty('host')) { - winston.error(' host'); - } - if (!ciVals.hasOwnProperty('port')) { - winston.error(' port'); - } - if (!ciVals.hasOwnProperty('database')) { - winston.error(' database'); - } + if (!ciVals.hasOwnProperty('port')) { + winston.error(' port'); + } - process.exit(); - } + if (!ciVals.hasOwnProperty('database')) { + winston.error(' database'); } + + process.exit(); } + async function setupConfig() { const configureDatabases = require('../install/databases');