mcollina/mosca — pull request #58 by justcfx2u (2013-09-21 18:57:29+00:00)
Title: Merge default options with config files if property not specified

At present, most mosca config file options do not specify a logger object.
That makes them break when, for example, trying to use verbose output mode.

Example `mosca.conf`:

module.exports = {
port: 1883,
backend: {
type: "redis"
}
};

Then run:

mosca -c mosca.conf -v

Will dump an error like:

$ mosca c ./mosca.conf -v

/usr/lib/node_modules/mosca/lib/cli.js:96
opts.logger.level = 30;
^
TypeError: Cannot set property 'level' of undefined
at /usr/lib/node_modules/mosca/lib/cli.js:96:25
at cli (/usr/lib/node_modules/mosca/lib/cli.js:232:12)
at Object. (/usr/lib/node_modules/mosca/bin/mosca:3:22)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3

This patch makes sure all "default" options are set to default values (merging default options with those specified in a config file) instead of outright replacing them, allowing configuration files to override only what is specified, while having a reasonable guarantee that the rest of the code which may depend on a default value can continue to do so. It also has the side-effect of making the current examples work better.