From baf3348cdac36beb0bac13f231d5f9878c4d031b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 15 Dec 2016 01:13:43 -0500 Subject: [PATCH 1/3] Normalize colors and content of the CLI tools --- src/command-line/add.js | 12 +++++++----- src/command-line/config.js | 5 +++-- src/command-line/edit.js | 7 ++++--- src/command-line/index.js | 3 ++- src/command-line/list.js | 7 ++++--- src/command-line/remove.js | 5 +++-- src/command-line/reset.js | 7 ++++--- src/command-line/start.js | 5 +++-- src/log.js | 5 +++++ 9 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/command-line/add.js b/src/command-line/add.js index c86bedd5..ee562f56 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -1,21 +1,22 @@ "use strict"; var ClientManager = new require("../clientManager"); +var colors = require("colors/safe"); var program = require("commander"); var Helper = require("../helper"); program .command("add ") .description("Add a new user") - .action(function(name/* , password */) { + .action(function(name) { var manager = new ClientManager(); var users = manager.getUsers(); if (users.indexOf(name) !== -1) { - log.error("User '" + name + "' already exists."); + log.error(`User ${colors.bold(name)} already exists.`); return; } require("read")({ - prompt: "[thelounge] Enter password: ", + prompt: log.rawInfo("Enter password: "), silent: true }, function(err, password) { if (!password) { @@ -34,6 +35,7 @@ function add(manager, name, password) { name, 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))}.`); } diff --git a/src/command-line/config.js b/src/command-line/config.js index d5ad2e1b..1eae72f2 100644 --- a/src/command-line/config.js +++ b/src/command-line/config.js @@ -2,11 +2,12 @@ var program = require("commander"); var child = require("child_process"); +var colors = require("colors/safe"); var Helper = require("../helper"); program .command("config") - .description("Edit config: " + Helper.CONFIG_PATH) + .description(`Edit configuration file located at ${colors.green(Helper.CONFIG_PATH)}.`) .action(function() { var child_spawn = child.spawn( process.env.EDITOR || "vi", @@ -14,6 +15,6 @@ program {stdio: "inherit"} ); 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.`); }); }); diff --git a/src/command-line/edit.js b/src/command-line/edit.js index dadd0e52..f33f9e1c 100644 --- a/src/command-line/edit.js +++ b/src/command-line/edit.js @@ -3,15 +3,16 @@ var ClientManager = new require("../clientManager"); var program = require("commander"); var child = require("child_process"); +var colors = require("colors/safe"); var Helper = require("../helper"); program .command("edit ") - .description("Edit user: " + Helper.getUserConfigPath("")) + .description(`Edit user file located at ${colors.green(Helper.getUserConfigPath(""))}.`) .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { - log.error("User '" + name + "' doesn't exist."); + log.error(`User ${colors.bold(name)} does not exist.`); return; } var child_spawn = child.spawn( @@ -20,6 +21,6 @@ program {stdio: "inherit"} ); 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.`); }); }); diff --git a/src/command-line/index.js b/src/command-line/index.js index 6df07f09..1149f939 100644 --- a/src/command-line/index.js +++ b/src/command-line/index.js @@ -3,6 +3,7 @@ global.log = require("../log.js"); var program = require("commander"); +var colors = require("colors/safe"); var fs = require("fs"); var fsextra = require("fs-extra"); var path = require("path"); @@ -26,7 +27,7 @@ if (!fs.existsSync(Helper.CONFIG_PATH)) { "defaults", "config.js" )), 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); diff --git a/src/command-line/list.js b/src/command-line/list.js index d8c07189..8020edda 100644 --- a/src/command-line/list.js +++ b/src/command-line/list.js @@ -2,6 +2,7 @@ var ClientManager = new require("../clientManager"); var program = require("commander"); +var colors = require("colors/safe"); program .command("list") @@ -9,11 +10,11 @@ program .action(function() { var users = new ClientManager().getUsers(); if (!users.length) { - log.warn("No users found!"); + log.warn("No users found."); } else { - console.log("Users:"); + log.info("Users:"); for (var i = 0; i < users.length; i++) { - console.log(" " + (i + 1) + ". " + users[i]); + log.info(`${i + 1}. ${colors.bold(users[i])}`); } } }); diff --git a/src/command-line/remove.js b/src/command-line/remove.js index efd9092d..3e90b658 100644 --- a/src/command-line/remove.js +++ b/src/command-line/remove.js @@ -2,6 +2,7 @@ var ClientManager = new require("../clientManager"); var program = require("commander"); +var colors = require("colors/safe"); program .command("remove ") @@ -9,8 +10,8 @@ program .action(function(name) { var manager = new ClientManager(); if (manager.removeUser(name)) { - log.info("Removed user '" + name + "'."); + log.info(`User ${colors.bold(name)} removed.`); } else { - log.error("User '" + name + "' doesn't exist."); + log.error(`User ${colors.bold(name)} does not exist.`); } }); diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 511bd554..9a21ff42 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -3,6 +3,7 @@ var ClientManager = new require("../clientManager"); var fs = require("fs"); var program = require("commander"); +var colors = require("colors/safe"); var Helper = require("../helper"); program @@ -11,13 +12,13 @@ program .action(function(name) { var users = new ClientManager().getUsers(); if (users.indexOf(name) === -1) { - log.error("User '" + name + "' doesn't exist."); + log.error(`User ${colors.bold(name)} does not exist.`); return; } var file = Helper.getUserConfigPath(name); var user = require(file); require("read")({ - prompt: "[thelounge] New password: ", + prompt: log.rawInfo("Enter new password: "), silent: true }, function(err, password) { if (err) { @@ -29,6 +30,6 @@ program file, JSON.stringify(user, null, "\t") ); - log.info("Successfully reset password for '" + name + "'."); + log.info(`Successfully reset password for ${colors.bold(name)}.`); }); }); diff --git a/src/command-line/start.js b/src/command-line/start.js index 7bd40a79..1f3b990c 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -2,6 +2,7 @@ var ClientManager = new require("../clientManager"); var program = require("commander"); +var colors = require("colors/safe"); var server = require("../server"); var Helper = require("../helper"); @@ -24,8 +25,8 @@ program } if (!mode && !users.length && !Helper.config.ldap.enable) { - log.warn("No users found!"); - log.info("Create a new user with 'lounge add '."); + log.warn("No users found."); + log.info(`Create a new user with ${colors.bold("lounge add ")}.`); return; } diff --git a/src/log.js b/src/log.js index 483d5345..948bc0b2 100644 --- a/src/log.js +++ b/src/log.js @@ -30,3 +30,8 @@ exports.info = function() { exports.debug = function() { console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); }; + +exports.rawInfo = function() { + const newArguments = timestamp(colors.blue("[INFO]"), arguments); + return Array.prototype.slice.call(newArguments).join(" "); +}; From 6c546b20989d5d8643f1426241216ffa3f14649d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Thu, 15 Dec 2016 01:29:44 -0500 Subject: [PATCH 2/3] Fix CLI options on the start command I know it is proposed to deprecate them in favor of a more generic one (`-c port=80`), but in the meantime the existing ones should work properly. --- src/command-line/start.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/command-line/start.js b/src/command-line/start.js index 1f3b990c..9a5fdbbd 100644 --- a/src/command-line/start.js +++ b/src/command-line/start.js @@ -7,20 +7,20 @@ var server = require("../server"); var Helper = require("../helper"); program + .command("start") .option("-H, --host ", "host") .option("-P, --port ", "port") .option("-B, --bind ", "bind") .option(" --public", "mode") .option(" --private", "mode") - .command("start") .description("Start the server") - .action(function() { + .action(function(options) { var users = new ClientManager().getUsers(); var mode = Helper.config.public; - if (program.public) { + if (options.public) { mode = true; - } else if (program.private) { + } else if (options.private) { mode = false; } @@ -31,9 +31,9 @@ program return; } - Helper.config.host = program.host || Helper.config.host; - Helper.config.port = program.port || Helper.config.port; - Helper.config.bind = program.bind || Helper.config.bind; + Helper.config.host = options.host || Helper.config.host; + Helper.config.port = options.port || Helper.config.port; + Helper.config.bind = options.bind || Helper.config.bind; Helper.config.public = mode; server(); From 770ede0587ee7b655181906245e9c642a0c8186c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Astori?= Date: Fri, 16 Dec 2016 02:27:41 -0500 Subject: [PATCH 3/3] Define a common helper for prompting to the user consistently --- src/command-line/add.js | 4 ++-- src/command-line/reset.js | 4 ++-- src/log.js | 7 ++++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/command-line/add.js b/src/command-line/add.js index ee562f56..613a02bd 100644 --- a/src/command-line/add.js +++ b/src/command-line/add.js @@ -15,8 +15,8 @@ program log.error(`User ${colors.bold(name)} already exists.`); return; } - require("read")({ - prompt: log.rawInfo("Enter password: "), + log.prompt({ + text: "Enter password:", silent: true }, function(err, password) { if (!password) { diff --git a/src/command-line/reset.js b/src/command-line/reset.js index 9a21ff42..8be036e7 100644 --- a/src/command-line/reset.js +++ b/src/command-line/reset.js @@ -17,8 +17,8 @@ program } var file = Helper.getUserConfigPath(name); var user = require(file); - require("read")({ - prompt: log.rawInfo("Enter new password: "), + log.prompt({ + text: "Enter new password:", silent: true }, function(err, password) { if (err) { diff --git a/src/log.js b/src/log.js index 948bc0b2..9a5ded3b 100644 --- a/src/log.js +++ b/src/log.js @@ -2,6 +2,7 @@ var colors = require("colors/safe"); var moment = require("moment"); +const read = require("read"); var Helper = require("./helper"); function timestamp(type, messageArgs) { @@ -31,7 +32,7 @@ exports.debug = function() { console.log.apply(console, timestamp(colors.green("[DEBUG]"), arguments)); }; -exports.rawInfo = function() { - const newArguments = timestamp(colors.blue("[INFO]"), arguments); - return Array.prototype.slice.call(newArguments).join(" "); +exports.prompt = (options, callback) => { + options.prompt = timestamp(colors.cyan("[PROMPT]"), [options.text]).join(" "); + read(options, callback); };