Skip to content

Commit

Permalink
trying to solve 22 to 15 complexity issue, other is solved i think
Browse files Browse the repository at this point in the history
  • Loading branch information
mrahmed2026 authored and mrahmed2026 committed Sep 3, 2024
1 parent 4691b1a commit 35f6a7f
Showing 1 changed file with 82 additions and 67 deletions.
149 changes: 82 additions & 67 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,80 +48,95 @@ questions.optional = [
];

function checkSetupFlagEnv() {
let setupVal = install.values;

const envConfMap = {
CONFIG: 'config',
NODEBB_CONFIG: 'config',
NODEBB_URL: 'url',
NODEBB_PORT: 'port',
NODEBB_ADMIN_USERNAME: 'admin:username',
NODEBB_ADMIN_PASSWORD: 'admin:password',
NODEBB_ADMIN_EMAIL: 'admin:email',
NODEBB_DB: 'database',
NODEBB_DB_HOST: 'host',
NODEBB_DB_PORT: 'port',
NODEBB_DB_USER: 'username',
NODEBB_DB_PASSWORD: 'password',
NODEBB_DB_NAME: 'database',
NODEBB_DB_SSL: 'ssl',
};
let setupVal = install.values;

Check failure on line 51 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
const envConfMap = getEnvConfMap();

Check failure on line 52 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

// 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;
}
});
if (hasRelevantEnvVars(envConfMap)) {

Check failure on line 54 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
winston.info('[install/checkSetupFlagEnv] checking env vars for setup info...');

Check failure on line 55 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
setupVal = setUpValuesFromEnv(setupVal, envConfMap);

Check failure on line 56 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
}

Check failure on line 57 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

setupVal['admin:password:confirm'] = setupVal['admin:password'];
}
setupVal = getSetupValuesFromJson(setupVal);

Check failure on line 59 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces

// 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
try {
if (nconf.get('setup')) {
const setupJSON = JSON.parse(nconf.get('setup'));
setupVal = { ...setupVal, ...setupJSON };
}
} catch (err) {
winston.error('[install/checkSetupFlagEnv] invalid json in nconf.get(\'setup\'), ignoring setup values from json');
}
if (setupVal && typeof setupVal === 'object') {

Check failure on line 61 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
validateSetupValues(setupVal);

Check failure on line 62 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces
} else if (nconf.get('database')) {

Check failure on line 63 in src/install.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 1 tab but found 4 spaces
setDatabaseValues();
}
}

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');
}
function getEnvConfMap() {
return {
CONFIG: 'config',
NODEBB_CONFIG: 'config',
NODEBB_URL: 'url',
NODEBB_PORT: 'port',
NODEBB_ADMIN_USERNAME: 'admin:username',
NODEBB_ADMIN_PASSWORD: 'admin:password',
NODEBB_ADMIN_EMAIL: 'admin:email',
NODEBB_DB: 'database',
NODEBB_DB_HOST: 'host',
NODEBB_DB_PORT: 'port',
NODEBB_DB_USER: 'username',
NODEBB_DB_PASSWORD: 'password',
NODEBB_DB_NAME: 'database',
NODEBB_DB_SSL: 'ssl',
};
}

process.exit();
}
} else if (nconf.get('database')) {
install.values = install.values || {};
install.values.database = nconf.get('database');
}
function hasRelevantEnvVars(envConfMap) {
const envKeys = Object.keys(process.env);
return Object.keys(envConfMap).some(key => envKeys.includes(key));
}

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;
}

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

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 = getCIVals();

Expand Down

0 comments on commit 35f6a7f

Please sign in to comment.