Shuo/index.js

122 lines
2.7 KiB
JavaScript
Raw Normal View History

2014-08-05 08:32:25 +00:00
#!/usr/bin/env node
process.chdir(__dirname);
2014-08-05 08:44:21 +00:00
var config = require("./config.json");
2014-08-14 01:51:54 +00:00
var ClientManager= new require("./src/clientManager");
2014-08-05 08:32:25 +00:00
var program = require("commander");
var shout = require("./src/server.js");
2014-08-16 16:15:17 +00:00
var fs = require("fs");
2014-08-05 08:44:21 +00:00
program
2014-08-17 13:10:27 +00:00
.option("-p, --port <port>");
2014-08-05 08:32:25 +00:00
program
.command("start")
2014-08-05 08:44:21 +00:00
.description("Start the server")
2014-08-05 08:32:25 +00:00
.action(function() {
2014-08-14 17:25:22 +00:00
var users = new ClientManager().getUsers();
2014-08-17 13:10:27 +00:00
if (!config.public && !users.length) {
2014-08-14 17:25:22 +00:00
console.log("");
console.log("No users found!");
console.log("Create a new user with 'shout add-user <name>'.")
console.log("");
} else {
2014-08-17 13:10:27 +00:00
var port = program.port || config.port;
shout(port, config.public);
2014-08-14 17:25:22 +00:00
}
2014-08-05 08:32:25 +00:00
});
2014-08-13 23:43:11 +00:00
program
2014-08-17 13:10:27 +00:00
.command("list")
2014-08-13 23:43:11 +00:00
.description("List all existing users")
.action(function() {
2014-08-14 17:25:22 +00:00
var users = new ClientManager().getUsers();
if (!users.length) {
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
console.log("No users found!");
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
} else {
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
console.log("Users:");
for (var i = 0; i < users.length; i++) {
2014-08-17 13:10:27 +00:00
console.log(" " + (i + 1) + ". " + users[i]);
2014-08-14 17:25:22 +00:00
}
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-13 23:43:11 +00:00
}
});
program
2014-08-17 13:10:27 +00:00
.command("add <name>")
2014-08-13 23:43:11 +00:00
.description("Add a new user")
.action(function(name) {
2014-08-16 16:15:17 +00:00
try {
var path = __dirname + "/users";
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);
} catch (e) {
console.log("");
console.log("You have no permissions to write to " + path);
console.log("Try running the command as sudo.");
console.log("");
return;
}
2014-08-13 23:43:11 +00:00
var manager = new ClientManager();
2014-08-14 17:25:22 +00:00
var users = manager.getUsers();
if (users.indexOf(name) !== -1) {
console.log("");
console.log("User '" + name + "' already exists.");
console.log("");
return;
}
2014-08-14 01:51:54 +00:00
require("read")({
prompt: "Password: "
}, function(err, password) {
2014-08-14 17:25:22 +00:00
console.log("");
2014-08-14 01:51:54 +00:00
if (err) {
return;
}
2014-08-14 17:25:22 +00:00
var success = manager.addUser(
2014-08-14 01:51:54 +00:00
name,
password
);
2014-08-14 17:25:22 +00:00
console.log("Added '" + name + "'.");
console.log("");
2014-08-14 01:51:54 +00:00
});
2014-08-13 23:43:11 +00:00
});
program
2014-08-17 13:10:27 +00:00
.command("remove <name>")
2014-08-13 23:43:11 +00:00
.description("Remove an existing user")
.action(function(name) {
2014-08-16 16:15:17 +00:00
try {
var path = __dirname + "/users";
var test = path + "/.test";
fs.mkdirSync(test);
fs.rmdirSync(test);
} catch (e) {
console.log("");
console.log("You have no permissions to delete from " + path);
console.log("Try running the command as sudo.");
console.log("");
return;
}
2014-08-13 23:43:11 +00:00
var manager = new ClientManager();
2014-08-14 17:25:22 +00:00
var success = manager.removeUser(name);
if (success) {
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
console.log("Removed '" + name + "'.");
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
} else {
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
console.log("User '" + name + "' doesn't exist.");
2014-08-16 16:15:17 +00:00
console.log("");
2014-08-14 17:25:22 +00:00
}
2014-08-13 23:43:11 +00:00
});
2014-08-05 08:32:25 +00:00
program.parse(process.argv)
if (!program.args.length) {
2014-08-17 13:10:27 +00:00
program.parse(process.argv.concat("start"));
2014-08-05 08:32:25 +00:00
}