mirror of
https://github.com/thelounge/thelounge
synced 2024-11-22 12:03:11 +00:00
Added ClientManager class
This commit is contained in:
parent
691e37b031
commit
c870dc8c15
2 changed files with 94 additions and 0 deletions
28
index.js
28
index.js
|
@ -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
66
src/clientManager.js
Normal 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 + "'."
|
||||
);
|
||||
};
|
Loading…
Reference in a new issue