Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Commit

Permalink
Added basic CLI support for adding and removing users.
Browse files Browse the repository at this point in the history
References #20.
  • Loading branch information
mcollina committed Apr 14, 2013
1 parent e237f59 commit 533e3c9
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 39 deletions.
139 changes: 102 additions & 37 deletions lib/cli.js
@@ -1,60 +1,125 @@
var pkg = require("../package");
var program = require("commander");
var commander = require("commander");
var path = require("path");

program
.version(pkg.version)
.option("-p, --port <n>", "the port to listen to", parseInt)
.option("--parent-port <n>", "the parent port to connect to", parseInt)
.option("--parent-host <s>", "the parent host to connect to")
.option("--parent-prefix <s>", "the prefix to use in the parent broker")
.option("-c, --config <c>", "the config file to use (override every other options)")
.option("-v, --verbose", "equal to DEBUG=mosca")
.option("--very-verbose", "equal to DEBUG=mosca,ascoltatori:*");
var Authorizer = require("./authorizer");
var fs = require("fs");

/**
* The basic command line interface of Mosca.
*
* @api private
*/
module.exports = function cli(argv) {
module.exports = function cli(argv, callback) {

argv = argv || [];

program.parse(argv);
var program = new commander.Command();
var server = null;
var runned = false;

if (program.veryVerbose) {
process.env.DEBUG = "mosca,ascoltatori:*";
} else if (program.verbose) {
process.env.DEBUG = "mosca";
}
program
.version(pkg.version)
.option("-p, --port <n>", "the port to listen to", parseInt)
.option("--parent-port <n>", "the parent port to connect to", parseInt)
.option("--parent-host <s>", "the parent host to connect to")
.option("--parent-prefix <s>", "the prefix to use in the parent broker")
.option("--credentials <file>", "the file containing the credentials", null, "./credentials.json")
.option("-c, --config <c>", "the config file to use (override every other options)")
.option("-v, --verbose", "equal to DEBUG=mosca")
.option("--very-verbose", "equal to DEBUG=mosca,ascoltatori:*");

var setupVerbose = function() {
runned = true;
if (program.veryVerbose) {
process.env.DEBUG = "mosca,ascoltatori:*";
} else if (program.verbose) {
process.env.DEBUG = "mosca";
}
};

var start = function() {
setupVerbose();

// this MUST be done after changing the DEBUG env
var Server = require("./server");

var opts = {
backend: {}
};
opts.port = program.port;

if (program.parentPort || program.parentHost) {
opts.backend.type = "mqtt";
opts.backend.port = 1883;
}

if (program.parentHost) {
opts.backend.host = program.parentHost;
}

if (program.parentPort) {
opts.backend.port = program.parentPort;
}

// this MUST be done after changing the DEBUG env
var Server = require("./server");
opts.backend.prefix = program.parentPrefix;

var opts = {
backend: {}
if (program.config) {
opts = require(path.join(process.cwd(), program.config));
}

server = new Server(opts);
};
opts.port = program.port;

if (program.parentPort || program.parentHost) {
opts.backend.type = "mqtt";
opts.backend.port = 1883;
}
var adduser = function (username, password) {
setupVerbose();

if (program.parentHost) {
opts.backend.host = program.parentHost;
}
var authorizer = new Authorizer();
try {
authorizer.users = JSON.parse(fs.readFileSync(program.credentials));
} catch (err) {
// nothing to do
}
authorizer.addUser(username, password, function() {
fs.writeFileSync(program.credentials, JSON.stringify(authorizer.users));
callback();
});
};

if (program.parentPort) {
opts.backend.port = program.parentPort;
}
var rmuser = function (username, password) {
setupVerbose();

opts.backend.prefix = program.parentPrefix;
var authorizer = new Authorizer();
try {
authorizer.users = JSON.parse(fs.readFileSync(program.credentials));
} catch (err) {
// nothing to do
}
authorizer.rmUser(username, function() {
fs.writeFileSync(program.credentials, JSON.stringify(authorizer.users));
callback();
});
};

program.
command("adduser <user> <pass>").
description("Add a user to the given credentials file").
action(adduser);

program.
command("rmuser <user>").
description("Removes a user from the given credentials file").
action(rmuser);

program.
command("start").
description("start the server (optional)").
action(start);

program.parse(argv);

if (program.config) {
opts = require(path.join(process.cwd(), program.config));
if (!runned) {
start();
}

return new Server(opts);
return server;
};
1 change: 1 addition & 0 deletions lib/server.js
Expand Up @@ -59,6 +59,7 @@ function Server(opts, callback) {
}

this.ascoltatore = ascoltatori.build(this.opts.backend);
this.ascoltatore.on("error", this.emit.bind(this));

that.once("ready", callback);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -40,7 +40,8 @@
"microtime": "~0.3.3",
"dox-foundation": "~0.4.4",
"jshint": "~1.0.0",
"js-beautify": "~0.4.2"
"js-beautify": "~0.4.2",
"tmp": "0.0.17"
},
"dependencies": {
"mqtt": "git://github.com/mcollina/MQTT.js.git#stream2-fix",
Expand Down
53 changes: 52 additions & 1 deletion test/cli_spec.js
@@ -1,4 +1,6 @@
var async = require("async");
var tmp = require('tmp');
var fs = require("fs");

describe("mosca.cli", function() {

Expand Down Expand Up @@ -31,7 +33,9 @@ describe("mosca.cli", function() {
function(cb) {
parentServer.close(cb);
}
], done);
], function() {
done();
});
});

it("must be a function", function() {
Expand Down Expand Up @@ -123,4 +127,51 @@ describe("mosca.cli", function() {
done();
});
});

it("should add an user to an authorization file", function(done) {
args.push("adduser");
args.push("myuser");
args.push("mypass");
args.push("--credentials");

tmp.file(function (err, path, fd) {
if (err) {
done(err);
return;
}

args.push(path);
mosca.cli(args, function () {
var content = JSON.parse(fs.readFileSync(path));
expect(content).to.have.property("myuser");
done();
});
});
});

it("should remove an user from an authorization file", function(done) {
args.push("adduser");
args.push("myuser");
args.push("mypass");
args.push("--credentials");

tmp.file(function (err, path, fd) {
if (err) {
done(err);
return;
}

args.push(path);
var cloned = [].concat(args);
cloned[2] = "rmuser";

mosca.cli(args, function () {
mosca.cli(cloned, function () {
var content = JSON.parse(fs.readFileSync(path));
expect(content).not.to.have.property("myuser");
done();
});
});
});
});
});

0 comments on commit 533e3c9

Please sign in to comment.