Skip to content

Commit

Permalink
Merge pull request thelounge#799 from thelounge/astorije/improve-comm…
Browse files Browse the repository at this point in the history
…and-line-output

Improve CLI a bit (output formatting and bug fix)
  • Loading branch information
xPaw authored Dec 19, 2016
2 parents 99ae61c + e7d299e commit e77ecac
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 30 deletions.
14 changes: 8 additions & 6 deletions src/command-line/add.js
Original file line number Diff line number Diff line change
@@ -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 <name>")
.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: ",
log.prompt({
text: "Enter password:",
silent: true
}, function(err, password) {
if (!password) {
Expand All @@ -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))}.`);
}
5 changes: 3 additions & 2 deletions src/command-line/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

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",
[Helper.CONFIG_PATH],
{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.`);
});
});
7 changes: 4 additions & 3 deletions src/command-line/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <name>")
.description("Edit user: " + Helper.getUserConfigPath("<name>"))
.description(`Edit user file located at ${colors.green(Helper.getUserConfigPath("<name>"))}.`)
.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(
Expand All @@ -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.`);
});
});
3 changes: 2 additions & 1 deletion src/command-line/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions src/command-line/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

var ClientManager = new require("../clientManager");
var program = require("commander");
var colors = require("colors/safe");

program
.command("list")
.description("List all users")
.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])}`);
}
}
});
5 changes: 3 additions & 2 deletions src/command-line/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

var ClientManager = new require("../clientManager");
var program = require("commander");
var colors = require("colors/safe");

program
.command("remove <name>")
.description("Remove an existing user")
.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.`);
}
});
9 changes: 5 additions & 4 deletions src/command-line/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: ",
log.prompt({
text: "Enter new password:",
silent: true
}, function(err, password) {
if (err) {
Expand All @@ -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)}.`);
});
});
19 changes: 10 additions & 9 deletions src/command-line/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,38 @@

var ClientManager = new require("../clientManager");
var program = require("commander");
var colors = require("colors/safe");
var server = require("../server");
var Helper = require("../helper");

program
.command("start")
.option("-H, --host <ip>", "host")
.option("-P, --port <port>", "port")
.option("-B, --bind <ip>", "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;
}

if (!mode && !users.length && !Helper.config.ldap.enable) {
log.warn("No users found!");
log.info("Create a new user with 'lounge add <name>'.");
log.warn("No users found.");
log.info(`Create a new user with ${colors.bold("lounge add <name>")}.`);

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();
Expand Down
6 changes: 6 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -30,3 +31,8 @@ exports.info = function() {
exports.debug = function() {
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);
};

0 comments on commit e77ecac

Please sign in to comment.