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")
.description("Start the server")
.action(function() {
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
.command("list-users")
.description("List all existing users")
.action(function() {
var manager = new ClientManager();
var users = manager.getUsers();
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((i + 1) + ": " + users[i]);
}
}
console.log("");
});
program
@ -34,17 +48,26 @@ program
.description("Add a new user")
.action(function(name) {
var manager = new ClientManager();
require("read")({
prompt: "Password: "
}, function(err, password) {
if (err) {
var users = manager.getUsers();
if (users.indexOf(name) !== -1) {
console.log("");
console.log("User '" + name + "' already exists.");
console.log("");
return;
}
manager.addUser(
require("read")({
prompt: "Password: "
}, function(err, password) {
console.log("");
if (err) {
return;
}
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)

View File

@ -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,

View File

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