Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring function Complexity from 22-15 and 16-15, src/install.js #70

Open
wants to merge 4 commits into
base: f24
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 91 additions & 61 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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');

Expand Down