Move cli code

This commit is contained in:
Mattias Erming 2014-08-18 18:18:40 -07:00
parent 0c34bb84b2
commit 5e9c032591
7 changed files with 132 additions and 120 deletions

120
index.js
View File

@ -1,121 +1,3 @@
#!/usr/bin/env node
process.chdir(__dirname);
var config = require("./config.json");
var ClientManager= new require("./src/clientManager");
var program = require("commander");
var shout = require("./src/server.js");
var fs = require("fs");
program
.option("-p, --port <port>");
program
.command("start")
.description("Start the server")
.action(function() {
var users = new ClientManager().getUsers();
if (!config.public && !users.length) {
console.log("");
console.log("No users found!");
console.log("Create a new user with 'shout add-user <name>'.")
console.log("");
} else {
var port = program.port || config.port;
shout(port, config.public);
}
});
program
.command("list")
.description("List all existing users")
.action(function() {
var users = new ClientManager().getUsers();
if (!users.length) {
console.log("");
console.log("No users found!");
console.log("");
} else {
console.log("");
console.log("Users:");
for (var i = 0; i < users.length; i++) {
console.log(" " + (i + 1) + ". " + users[i]);
}
console.log("");
}
});
program
.command("add <name>")
.description("Add a new user")
.action(function(name) {
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;
}
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) {
return;
}
var success = manager.addUser(
name,
password
);
console.log("Added '" + name + "'.");
console.log("");
});
});
program
.command("remove <name>")
.description("Remove an existing user")
.action(function(name) {
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;
}
var manager = new ClientManager();
var success = manager.removeUser(name);
if (success) {
console.log("");
console.log("Removed '" + name + "'.");
console.log("");
} else {
console.log("");
console.log("User '" + name + "' doesn't exist.");
console.log("");
}
});
program.parse(process.argv)
if (!program.args.length) {
program.parse(process.argv.concat("start"));
}
var cli = require("./src/cli");

View File

@ -1,7 +1,7 @@
{
"name": "shout",
"description": "A web IRC client",
"version": "0.10.3",
"version": "0.10.4",
"author": "Mattias Erming",
"preferGlobal": true,
"bin": {

43
src/cli/add.js Normal file
View File

@ -0,0 +1,43 @@
var ClientManager = new require("../clientManager");
var fs = require("fs");
var program = require("commander");
program
.command("add <name>")
.description("Add a new user")
.action(function(name) {
try {
var path = process.cwd() + "/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;
}
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) {
return;
}
var success = manager.addUser(
name,
password
);
console.log("Added '" + name + "'.");
console.log("");
});
});

14
src/cli/index.js Normal file
View File

@ -0,0 +1,14 @@
var program = require("commander");
require("./start");
require("./list");
require("./add");
require("./remove");
program
.option("-p, --port <port>")
.parse(process.argv);
if (!program.args.length) {
program.parse(process.argv.concat("start"));
}

21
src/cli/list.js Normal file
View File

@ -0,0 +1,21 @@
var ClientManager = new require("../clientManager");
var program = require("commander");
program
.command("list")
.description("List all existing users")
.action(function() {
var users = new ClientManager().getUsers();
if (!users.length) {
console.log("");
console.log("No users found!");
console.log("");
} else {
console.log("");
console.log("Users:");
for (var i = 0; i < users.length; i++) {
console.log(" " + (i + 1) + ". " + users[i]);
}
console.log("");
}
});

32
src/cli/remove.js Normal file
View File

@ -0,0 +1,32 @@
var ClientManager = new require("../clientManager");
var fs = require("fs");
var program = require("commander");
program
.command("remove <name>")
.description("Remove an existing user")
.action(function(name) {
try {
var path = process.cwd() + "/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;
}
var manager = new ClientManager();
var success = manager.removeUser(name);
if (success) {
console.log("");
console.log("Removed '" + name + "'.");
console.log("");
} else {
console.log("");
console.log("User '" + name + "' doesn't exist.");
console.log("");
}
});

20
src/cli/start.js Normal file
View File

@ -0,0 +1,20 @@
var config = require("../../config.json");
var ClientManager = new require("../clientManager");
var program = require("commander");
var shout = require("../server");
program
.command("start")
.description("Start the server")
.action(function() {
var users = new ClientManager().getUsers();
if (!config.public && !users.length) {
console.log("");
console.log("No users found!");
console.log("Create a new user with 'shout add-user <name>'.")
console.log("");
} else {
var port = program.port || config.port;
shout(port, config.public);
}
});