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

Refactor this function to reduce its Cognitive Complexity from 22 to the 15 allowed #176

Open
wants to merge 1 commit into
base: main
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
77 changes: 37 additions & 40 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,56 +69,52 @@ function checkSetupFlagEnv() {

// Set setup values from env vars (if set)
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;
}
});

setupVal['admin:password:confirm'] = setupVal['admin:password'];
if (!Object.keys(envConfMap).some(key => envKeys.includes(key))) {
return;
}

winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...');
setupVal = setupVal || {};

// Process relevant environment variables
const processEnvVariable = (evName, evValue) => {
if (evName.startsWith('NODEBB_DB_')) {
setupVal[`${process.env.NODEBB_DB}:${envConfMap[evName]}`] = evValue;
} else if (evName.startsWith('NODEBB_')) {
setupVal[envConfMap[evName]] = evValue;
}
};

Object.entries(process.env).forEach(([evName, evValue]) => {
processEnvVariable(evName, evValue);
});

// Confirm admin password setup
setupVal['admin:password:confirm'] = setupVal['admin:password'];

// 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
const setupData = nconf.get('setup');
if (!setupData) return;

try {
if (nconf.get('setup')) {
const setupJSON = JSON.parse(nconf.get('setup'));
setupVal = { ...setupVal, ...setupJSON };
}
const setupJSON = JSON.parse(setupData);
setupVal = { ...setupVal, ...setupJSON };
} catch (err) {
winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json');
winston.error('[install/checkSetupFlagEnv] invalid JSON in nconf.get(\'setup\'), ignoring setup values from JSON');
}

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');
}
if (!setupVal || typeof setupVal !== 'object') return;

process.exit();
}
} else if (nconf.get('database')) {
install.values = install.values || {};
install.values.database = nconf.get('database');
const requiredFields = ['admin:username', 'admin:password', 'admin:password:confirm', 'admin:email'];
const missingFields = requiredFields.filter(field => !setupVal[field]);

if (missingFields.length === 0) {
install.values = setupVal;
} else {
winston.error('[install/checkSetupFlagEnv] required values are missing for automated setup:');
missingFields.forEach(field => winston.error(` ${field}`));
}
}

Expand Down Expand Up @@ -261,6 +257,7 @@ async function enableDefaultTheme() {
});
}


async function createDefaultUserGroups() {
const groups = require('./groups');
async function createGroup(name) {
Expand Down
Loading