Merge pull request #3470 from thelounge/xpaw/first-run-crash

Do not crash on first run due to config.js not existing
This commit is contained in:
Pavel Djundik 2019-10-23 12:33:33 +03:00 committed by GitHub
commit 5ba5505ba4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -29,31 +29,11 @@ const argvWithoutOptions = program.parseOptions(process.argv);
Helper.setHome(process.env.THELOUNGE_HOME || Utils.defaultHome()); Helper.setHome(process.env.THELOUNGE_HOME || Utils.defaultHome());
// Check config file owner and warn if we're running under a different user // Check config file owner and warn if we're running under a different user
if (process.getuid) { try {
const uid = process.getuid(); verifyFileOwner();
} catch (e) {
if (uid === 0) { // We do not care about failures of these checks
log.warn( // fs.statSync will throw if config.js does not exist (e.g. first run)
`You are currently running The Lounge as root. ${colors.bold.red(
"We highly discourage running as root!"
)}`
);
}
const configStat = fs.statSync(path.join(Helper.getHomePath(), "config.js"));
if (configStat && configStat.uid !== uid) {
log.warn(
"Config file owner does not match the user you are currently running The Lounge as."
);
log.warn(
"To prevent any issues, please run thelounge commands " +
"as the correct user that owns the config folder."
);
log.warn(
"See https://thelounge.chat/docs/usage#using-the-correct-system-user for more information."
);
}
} }
Utils.checkOldHome(); Utils.checkOldHome();
@ -83,3 +63,34 @@ program.parse(argvWithoutOptions.args.concat(argvWithoutOptions.unknown));
if (!program.args.length) { if (!program.args.length) {
program.help(); program.help();
} }
function verifyFileOwner() {
if (!process.getuid) {
return;
}
const uid = process.getuid();
if (uid === 0) {
log.warn(
`You are currently running The Lounge as root. ${colors.bold.red(
"We highly discourage running as root!"
)}`
);
}
const configStat = fs.statSync(path.join(Helper.getHomePath(), "config.js"));
if (configStat && configStat.uid !== uid) {
log.warn(
"Config file owner does not match the user you are currently running The Lounge as."
);
log.warn(
"To prevent any issues, please run thelounge commands " +
"as the correct user that owns the config folder."
);
log.warn(
"See https://thelounge.chat/docs/usage#using-the-correct-system-user for more information."
);
}
}