Updated the cli

This commit is contained in:
Mattias Erming 2014-08-14 10:25:22 -07:00
parent 27071e0a92
commit 67375a2600
3 changed files with 48 additions and 22 deletions

View File

@ -15,18 +15,32 @@ program
.command("start") .command("start")
.description("Start the server") .description("Start the server")
.action(function() { .action(function() {
shout(program.port, program.public); var users = new ClientManager().getUsers();
if (!program.public && !users.length) {
console.log("");
console.log("No users found!");
console.log("Create a new user with 'shout add-user <name>'.")
console.log("");
} else {
shout(program.port, program.public);
}
}); });
program program
.command("list-users") .command("list-users")
.description("List all existing users") .description("List all existing users")
.action(function() { .action(function() {
var manager = new ClientManager(); var users = new ClientManager().getUsers();
var users = manager.getUsers(); console.log("");
for (var i = 0; i < users.length; i++) { if (!users.length) {
console.log((i + 1) + " " + users[i]); console.log("No users found!");
} else {
console.log("Users:");
for (var i = 0; i < users.length; i++) {
console.log((i + 1) + ": " + users[i]);
}
} }
console.log("");
}); });
program program
@ -34,17 +48,26 @@ program
.description("Add a new user") .description("Add a new user")
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); var manager = new ClientManager();
var users = manager.getUsers();
if (users.indexOf(name) !== -1) {
console.log("");
console.log("User '" + name + "' already exists.");
console.log("");
return;
}
require("read")({ require("read")({
prompt: "Password: " prompt: "Password: "
}, function(err, password) { }, function(err, password) {
console.log("");
if (err) { if (err) {
console.log("");
return; return;
} }
manager.addUser( var success = manager.addUser(
name, name,
password password
); );
console.log("Added '" + name + "'.");
console.log("");
}); });
}); });
@ -53,7 +76,14 @@ program
.description("Remove an existing user") .description("Remove an existing user")
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); var manager = new ClientManager();
manager.removeUser(name); var success = manager.removeUser(name);
console.log("");
if (success) {
console.log("Removed '" + name + "'.");
} else {
console.log("User '" + name + "' doesn't exist.");
}
console.log("");
}); });
program.parse(process.argv) program.parse(process.argv)

View File

@ -1,7 +1,7 @@
{ {
"name": "shout", "name": "shout",
"description": "A web IRC client", "description": "A web IRC client",
"version": "0.9.4", "version": "0.9.5",
"homepage": "http://github.com/erming/shout", "homepage": "http://github.com/erming/shout",
"author": "Mattias Erming", "author": "Mattias Erming",
"preferGlobal": true, "preferGlobal": true,

View File

@ -1,3 +1,4 @@
var _ = require("lodash");
var fs = require("fs"); var fs = require("fs");
var Client = require("./client"); var Client = require("./client");
@ -20,9 +21,6 @@ ClientManager.prototype.findClient = function(name) {
ClientManager.prototype.loadUsers = function(sockets) { ClientManager.prototype.loadUsers = function(sockets) {
var users = this.getUsers(); var users = this.getUsers();
for (var i in users) { for (var i in users) {
if (name == "example") {
continue;
}
var name = users[i]; var name = users[i];
var json = this.loadUser(name); var json = this.loadUser(name);
if (!json) { if (!json) {
@ -56,14 +54,17 @@ ClientManager.prototype.getUsers = function() {
console.log(e); console.log(e);
return; return;
} }
users = _.without(
users,
"example"
);
return users; return users;
}; };
ClientManager.prototype.addUser = function(name, password) { ClientManager.prototype.addUser = function(name, password) {
var users = this.getUsers(); var users = this.getUsers();
if (users.indexOf(name) !== -1) { if (users.indexOf(name) !== -1) {
console.log("User '" + name + "' already exist."); return false;
return;
} }
try { try {
var path = "users/" + name; var path = "users/" + name;
@ -80,16 +81,13 @@ ClientManager.prototype.addUser = function(name, password) {
} catch(e) { } catch(e) {
throw e; throw e;
} }
console.log( return true;
"Added '" + name + "'."
);
}; };
ClientManager.prototype.removeUser = function(name) { ClientManager.prototype.removeUser = function(name) {
var users = this.getUsers(); var users = this.getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
console.log("User '" + name + "' doesn't exist."); return false;
return;
} }
try { try {
var path = "users/" + name; var path = "users/" + name;
@ -98,7 +96,5 @@ ClientManager.prototype.removeUser = function(name) {
} catch(e) { } catch(e) {
throw e; throw e;
} }
console.log( return true;
"Removed '" + name + "'."
);
}; };