Merge pull request #799 from thelounge/astorije/improve-command-line-output

Improve CLI a bit (output formatting and bug fix)
This commit is contained in:
Pavel Djundik 2016-12-19 14:18:54 +02:00 committed by GitHub
commit b9047d98a2
9 changed files with 45 additions and 30 deletions

View File

@ -1,21 +1,22 @@
"use strict"; "use strict";
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var colors = require("colors/safe");
var program = require("commander"); var program = require("commander");
var Helper = require("../helper"); var Helper = require("../helper");
program program
.command("add <name>") .command("add <name>")
.description("Add a new user") .description("Add a new user")
.action(function(name/* , password */) { .action(function(name) {
var manager = new ClientManager(); var manager = new ClientManager();
var users = manager.getUsers(); var users = manager.getUsers();
if (users.indexOf(name) !== -1) { if (users.indexOf(name) !== -1) {
log.error("User '" + name + "' already exists."); log.error(`User ${colors.bold(name)} already exists.`);
return; return;
} }
require("read")({ log.prompt({
prompt: "[thelounge] Enter password: ", text: "Enter password:",
silent: true silent: true
}, function(err, password) { }, function(err, password) {
if (!password) { if (!password) {
@ -34,6 +35,7 @@ function add(manager, name, password) {
name, name,
hash hash
); );
log.info("User '" + name + "' created:");
log.info(Helper.getUserConfigPath(name)); log.info(`User ${colors.bold(name)} created.`);
log.info(`User file located at ${colors.green(Helper.getUserConfigPath(name))}.`);
} }

View File

@ -2,11 +2,12 @@
var program = require("commander"); var program = require("commander");
var child = require("child_process"); var child = require("child_process");
var colors = require("colors/safe");
var Helper = require("../helper"); var Helper = require("../helper");
program program
.command("config") .command("config")
.description("Edit config: " + Helper.CONFIG_PATH) .description(`Edit configuration file located at ${colors.green(Helper.CONFIG_PATH)}.`)
.action(function() { .action(function() {
var child_spawn = child.spawn( var child_spawn = child.spawn(
process.env.EDITOR || "vi", process.env.EDITOR || "vi",
@ -14,6 +15,6 @@ program
{stdio: "inherit"} {stdio: "inherit"}
); );
child_spawn.on("error", function() { child_spawn.on("error", function() {
log.error("Unable to open " + Helper.CONFIG_PATH + ". $EDITOR is not set, and vi was not found."); log.error(`Unable to open ${colors.green(Helper.CONFIG_PATH)}. ${colors.bold("$EDITOR")} is not set, and ${colors.bold("vi")} was not found.`);
}); });
}); });

View File

@ -3,15 +3,16 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var program = require("commander"); var program = require("commander");
var child = require("child_process"); var child = require("child_process");
var colors = require("colors/safe");
var Helper = require("../helper"); var Helper = require("../helper");
program program
.command("edit <name>") .command("edit <name>")
.description("Edit user: " + Helper.getUserConfigPath("<name>")) .description(`Edit user file located at ${colors.green(Helper.getUserConfigPath("<name>"))}.`)
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
log.error("User '" + name + "' doesn't exist."); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;
} }
var child_spawn = child.spawn( var child_spawn = child.spawn(
@ -20,6 +21,6 @@ program
{stdio: "inherit"} {stdio: "inherit"}
); );
child_spawn.on("error", function() { child_spawn.on("error", function() {
log.error("Unable to open " + Helper.getUserConfigPath(name) + ". $EDITOR is not set, and vi was not found."); log.error(`Unable to open ${colors.green(Helper.getUserConfigPath(name))}. ${colors.bold("$EDITOR")} is not set, and ${colors.bold("vi")} was not found.`);
}); });
}); });

View File

@ -3,6 +3,7 @@
global.log = require("../log.js"); global.log = require("../log.js");
var program = require("commander"); var program = require("commander");
var colors = require("colors/safe");
var fs = require("fs"); var fs = require("fs");
var fsextra = require("fs-extra"); var fsextra = require("fs-extra");
var path = require("path"); var path = require("path");
@ -26,7 +27,7 @@ if (!fs.existsSync(Helper.CONFIG_PATH)) {
"defaults", "defaults",
"config.js" "config.js"
)), Helper.CONFIG_PATH); )), Helper.CONFIG_PATH);
log.info("Config created:", Helper.CONFIG_PATH); log.info(`Configuration file created at ${colors.green(Helper.CONFIG_PATH)}.`);
} }
fsextra.ensureDirSync(Helper.USERS_PATH); fsextra.ensureDirSync(Helper.USERS_PATH);

View File

@ -2,6 +2,7 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var program = require("commander"); var program = require("commander");
var colors = require("colors/safe");
program program
.command("list") .command("list")
@ -9,11 +10,11 @@ program
.action(function() { .action(function() {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (!users.length) { if (!users.length) {
log.warn("No users found!"); log.warn("No users found.");
} else { } else {
console.log("Users:"); log.info("Users:");
for (var i = 0; i < users.length; i++) { for (var i = 0; i < users.length; i++) {
console.log(" " + (i + 1) + ". " + users[i]); log.info(`${i + 1}. ${colors.bold(users[i])}`);
} }
} }
}); });

View File

@ -2,6 +2,7 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var program = require("commander"); var program = require("commander");
var colors = require("colors/safe");
program program
.command("remove <name>") .command("remove <name>")
@ -9,8 +10,8 @@ program
.action(function(name) { .action(function(name) {
var manager = new ClientManager(); var manager = new ClientManager();
if (manager.removeUser(name)) { if (manager.removeUser(name)) {
log.info("Removed user '" + name + "'."); log.info(`User ${colors.bold(name)} removed.`);
} else { } else {
log.error("User '" + name + "' doesn't exist."); log.error(`User ${colors.bold(name)} does not exist.`);
} }
}); });

View File

@ -3,6 +3,7 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var fs = require("fs"); var fs = require("fs");
var program = require("commander"); var program = require("commander");
var colors = require("colors/safe");
var Helper = require("../helper"); var Helper = require("../helper");
program program
@ -11,13 +12,13 @@ program
.action(function(name) { .action(function(name) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
if (users.indexOf(name) === -1) { if (users.indexOf(name) === -1) {
log.error("User '" + name + "' doesn't exist."); log.error(`User ${colors.bold(name)} does not exist.`);
return; return;
} }
var file = Helper.getUserConfigPath(name); var file = Helper.getUserConfigPath(name);
var user = require(file); var user = require(file);
require("read")({ log.prompt({
prompt: "[thelounge] New password: ", text: "Enter new password:",
silent: true silent: true
}, function(err, password) { }, function(err, password) {
if (err) { if (err) {
@ -29,6 +30,6 @@ program
file, file,
JSON.stringify(user, null, "\t") JSON.stringify(user, null, "\t")
); );
log.info("Successfully reset password for '" + name + "'."); log.info(`Successfully reset password for ${colors.bold(name)}.`);
}); });
}); });

View File

@ -2,37 +2,38 @@
var ClientManager = new require("../clientManager"); var ClientManager = new require("../clientManager");
var program = require("commander"); var program = require("commander");
var colors = require("colors/safe");
var server = require("../server"); var server = require("../server");
var Helper = require("../helper"); var Helper = require("../helper");
program program
.command("start")
.option("-H, --host <ip>", "host") .option("-H, --host <ip>", "host")
.option("-P, --port <port>", "port") .option("-P, --port <port>", "port")
.option("-B, --bind <ip>", "bind") .option("-B, --bind <ip>", "bind")
.option(" --public", "mode") .option(" --public", "mode")
.option(" --private", "mode") .option(" --private", "mode")
.command("start")
.description("Start the server") .description("Start the server")
.action(function() { .action(function(options) {
var users = new ClientManager().getUsers(); var users = new ClientManager().getUsers();
var mode = Helper.config.public; var mode = Helper.config.public;
if (program.public) { if (options.public) {
mode = true; mode = true;
} else if (program.private) { } else if (options.private) {
mode = false; mode = false;
} }
if (!mode && !users.length && !Helper.config.ldap.enable) { if (!mode && !users.length && !Helper.config.ldap.enable) {
log.warn("No users found!"); log.warn("No users found.");
log.info("Create a new user with 'lounge add <name>'."); log.info(`Create a new user with ${colors.bold("lounge add <name>")}.`);
return; return;
} }
Helper.config.host = program.host || Helper.config.host; Helper.config.host = options.host || Helper.config.host;
Helper.config.port = program.port || Helper.config.port; Helper.config.port = options.port || Helper.config.port;
Helper.config.bind = program.bind || Helper.config.bind; Helper.config.bind = options.bind || Helper.config.bind;
Helper.config.public = mode; Helper.config.public = mode;
server(); server();

View File

@ -2,6 +2,7 @@
var colors = require("colors/safe"); var colors = require("colors/safe");
var moment = require("moment"); var moment = require("moment");
const read = require("read");
var Helper = require("./helper"); var Helper = require("./helper");
function timestamp(type, messageArgs) { function timestamp(type, messageArgs) {
@ -30,3 +31,8 @@ exports.info = function() {
exports.debug = function() { exports.debug = function() {
console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments));
}; };
exports.prompt = (options, callback) => {
options.prompt = timestamp(colors.cyan("[PROMPT]"), [options.text]).join(" ");
read(options, callback);
};