Added ClientManager class

This commit is contained in:
Mattias Erming 2014-08-13 16:43:11 -07:00
parent 691e37b031
commit c870dc8c15
2 changed files with 94 additions and 0 deletions

View File

@ -4,6 +4,7 @@ process.chdir(__dirname);
var config = require("./config.json");
var program = require("commander");
var ClientManager = require("./src/clientManager");
var shout = require("./src/server.js");
program
@ -17,6 +18,33 @@ program
shout(port);
});
program
.command("list-users")
.description("List all existing users")
.action(function() {
var manager = new ClientManager();
var users = manager.getUsers();
for (var u in users) {
console.log(users[u]);
}
});
program
.command("add-user <name>")
.description("Add a new user")
.action(function(name) {
var manager = new ClientManager();
manager.addUser(name);
});
program
.command("remove-user <name>")
.description("Remove an existing user")
.action(function(name) {
var manager = new ClientManager();
manager.removeUser(name);
});
program.parse(process.argv)
if (!program.args.length) {

66
src/clientManager.js Normal file
View File

@ -0,0 +1,66 @@
var _ = require("lodash");
var fs = require("fs");
module.exports = ClientManager;
function ClientManager() {
this.clients = [];
}
ClientManager.prototype.loadUsers = function() {
var users = this.getUsers();
_.each(users, function(user) {
console.log(user);
});
};
ClientManager.prototype.getUsers = function() {
var users = [];
try {
users = fs.readdirSync("users/");
} catch(e) {
console.log(e);
return;
}
return users;
};
ClientManager.prototype.addUser = function(name) {
var users = this.getUsers();
if (users.indexOf(name) !== -1) {
console.log("User '" + name + "' already exist.");
return;
}
try {
var path = "users/" + name;
fs.mkdirSync(path);
fs.writeFileSync(path + "/user.json", "{}");
} catch(e) {
throw e;
}
console.log(
"Added '" + name + "'."
);
};
ClientManager.prototype.removeUser = function(name) {
var users = this.getUsers();
if (users.indexOf(name) === -1) {
console.log("User '" + name + "' doesn't exist.");
return;
}
try {
var path = "users/" + name;
fs.unlinkSync(path + "/user.json");
fs.rmdirSync(path);
} catch(e) {
throw e;
}
console.log(
"Removed '" + name + "'."
);
};