Make sure CLI does not crash on undefined.length when we figured out users could not be read already

This commit is contained in:
Jérémie Astori 2017-08-23 01:42:59 -04:00
parent cdbefd3905
commit ed68ff4a34
No known key found for this signature in database
GPG Key ID: B9A4F245CD67BDE8
6 changed files with 36 additions and 6 deletions

View File

@ -35,6 +35,12 @@ ClientManager.prototype.autoloadUsers = function() {
const users = this.getUsers(); const users = this.getUsers();
const noUsersWarning = `There are currently no users. Create one with ${colors.bold("lounge add <name>")}.`; const noUsersWarning = `There are currently no users. Create one with ${colors.bold("lounge add <name>")}.`;
// There was an error, already logged, but we have to crash the server as
// user directory could not be accessed
if (users === undefined) {
process.exit(1);
}
if (!users.length) { if (!users.length) {
log.info(noUsersWarning); log.info(noUsersWarning);
} }
@ -91,7 +97,7 @@ ClientManager.prototype.getUsers = function() {
} }
}); });
} catch (e) { } catch (e) {
log.error("Failed to get users", e); log.error(`Failed to get users (${e})`);
return; return;
} }
return users; return users;

View File

@ -18,6 +18,10 @@ program
const manager = new ClientManager(); const manager = new ClientManager();
const users = manager.getUsers(); const users = manager.getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) !== -1) { if (users.indexOf(name) !== -1) {
log.error(`User ${colors.bold(name)} already exists.`); log.error(`User ${colors.bold(name)} already exists.`);
return; return;

View File

@ -13,6 +13,11 @@ program
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
log.error(`User ${colors.bold(name)} does not exist.`); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;

View File

@ -16,6 +16,11 @@ program
} }
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (!users.length) { if (!users.length) {
log.info(`There are currently no users. Create one with ${colors.bold("lounge add <name>")}.`); log.info(`There are currently no users. Create one with ${colors.bold("lounge add <name>")}.`);
} else { } else {

View File

@ -10,10 +10,15 @@ program
.description("Remove an existing user") .description("Remove an existing user")
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); const manager = new ClientManager();
if (manager.removeUser(name)) {
log.info(`User ${colors.bold(name)} removed.`); try {
} else { if (manager.removeUser(name)) {
log.error(`User ${colors.bold(name)} does not exist.`); log.info(`User ${colors.bold(name)} removed.`);
} else {
log.error(`User ${colors.bold(name)} does not exist.`);
}
} catch (e) {
// There was an error, already logged
} }
}); });

View File

@ -13,6 +13,11 @@ program
.on("--help", Utils.extraHelp) .on("--help", Utils.extraHelp)
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users === undefined) { // There was an error, already logged
return;
}
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
log.error(`User ${colors.bold(name)} does not exist.`); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;