From 67375a260089a3ef42230e973acda07b69e69b07 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Thu, 14 Aug 2014 10:25:22 -0700 Subject: [PATCH] Updated the cli --- index.js | 46 ++++++++++++++++++++++++++++++++++++-------- package.json | 2 +- src/clientManager.js | 22 +++++++++------------ 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 31b47f81..978b0930 100755 --- a/index.js +++ b/index.js @@ -15,18 +15,32 @@ program .command("start") .description("Start the server") .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 '.") + console.log(""); + } else { + shout(program.port, program.public); + } }); program .command("list-users") .description("List all existing users") .action(function() { - var manager = new ClientManager(); - var users = manager.getUsers(); - for (var i = 0; i < users.length; i++) { - console.log((i + 1) + " " + users[i]); + var users = new ClientManager().getUsers(); + console.log(""); + if (!users.length) { + 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 @@ -34,17 +48,26 @@ program .description("Add a new user") .action(function(name) { 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")({ prompt: "Password: " }, function(err, password) { + console.log(""); if (err) { - console.log(""); return; } - manager.addUser( + var success = manager.addUser( name, password ); + console.log("Added '" + name + "'."); + console.log(""); }); }); @@ -53,7 +76,14 @@ program .description("Remove an existing user") .action(function(name) { 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) diff --git a/package.json b/package.json index 15961003..a30db96c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "shout", "description": "A web IRC client", - "version": "0.9.4", + "version": "0.9.5", "homepage": "http://github.com/erming/shout", "author": "Mattias Erming", "preferGlobal": true, diff --git a/src/clientManager.js b/src/clientManager.js index a168a7c0..f28537e8 100644 --- a/src/clientManager.js +++ b/src/clientManager.js @@ -1,3 +1,4 @@ +var _ = require("lodash"); var fs = require("fs"); var Client = require("./client"); @@ -20,9 +21,6 @@ ClientManager.prototype.findClient = function(name) { ClientManager.prototype.loadUsers = function(sockets) { var users = this.getUsers(); for (var i in users) { - if (name == "example") { - continue; - } var name = users[i]; var json = this.loadUser(name); if (!json) { @@ -56,14 +54,17 @@ ClientManager.prototype.getUsers = function() { console.log(e); return; } + users = _.without( + users, + "example" + ); return users; }; ClientManager.prototype.addUser = function(name, password) { var users = this.getUsers(); if (users.indexOf(name) !== -1) { - console.log("User '" + name + "' already exist."); - return; + return false; } try { var path = "users/" + name; @@ -80,16 +81,13 @@ ClientManager.prototype.addUser = function(name, password) { } catch(e) { throw e; } - console.log( - "Added '" + name + "'." - ); + return true; }; ClientManager.prototype.removeUser = function(name) { var users = this.getUsers(); if (users.indexOf(name) === -1) { - console.log("User '" + name + "' doesn't exist."); - return; + return false; } try { var path = "users/" + name; @@ -98,7 +96,5 @@ ClientManager.prototype.removeUser = function(name) { } catch(e) { throw e; } - console.log( - "Removed '" + name + "'." - ); + return true; };