From da0ec4f77a409799e672142b46c8abfa29fa0507 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Tue, 4 Feb 2014 15:30:19 +0000 Subject: [PATCH] Add ability to specify configuration file on the command line Resolves #105 --- server/configuration.js | 60 +++++++++++++++++++++++++++-------------- server/kiwi.js | 13 ++++++++- server/server.js | 4 +-- 3 files changed, 54 insertions(+), 23 deletions(-) diff --git a/server/configuration.js b/server/configuration.js index bc05e6e..b8241f3 100644 --- a/server/configuration.js +++ b/server/configuration.js @@ -12,33 +12,53 @@ var Config = function () { }; util.inherits(Config, events.EventEmitter); -Config.prototype.loadConfig = function () { +Config.prototype.loadConfig = function (manual_config_file) { var new_config, conf_filepath, i; - // Loop through the possible config paths and find a usable one - for (i = 0; i < config_dirs.length; i++) { - conf_filepath = config_dirs[i] + config_filename; + if (manual_config_file) { + if (fs.existsSync(manual_config_file)) { + try { + if (fs.lstatSync(manual_config_file).isFile() === true) { + // Clear the loaded config cache + delete require.cache[require.resolve(manual_config_file)]; - try { - if (fs.lstatSync(conf_filepath).isFile() === true) { - // Clear the loaded config cache - delete require.cache[require.resolve(conf_filepath)]; - - // Try load the new config file - new_config = require(conf_filepath); - break; + // Try load the new config file + new_config = require(manual_config_file); + } + } catch (e) { + console.log('An error occured parsing the config file ' + manual_config_file + ': ' + e.message); + process.exit(1); } - } catch (e) { - switch (e.code) { - case 'ENOENT': // No file/dir - break; - default: - console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message); - return false; + } else { + console.log('Could not find config file ' + manual_config_file); + process.exit(1); + } + } else { + // Loop through the possible config paths and find a usable one + for (i = 0; i < config_dirs.length; i++) { + conf_filepath = config_dirs[i] + config_filename; + + try { + if (fs.lstatSync(conf_filepath).isFile() === true) { + // Clear the loaded config cache + delete require.cache[require.resolve(conf_filepath)]; + + // Try load the new config file + new_config = require(conf_filepath); + break; + } + } catch (e) { + switch (e.code) { + case 'ENOENT': // No file/dir + break; + default: + console.log('An error occured parsing the config file ' + config_dirs[i] + config_filename + ': ' + e.message); + return false; + } + continue; } - continue; } } diff --git a/server/kiwi.js b/server/kiwi.js index cd25155..44c267e 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -11,7 +11,18 @@ var fs = require('fs'), process.chdir(__dirname + '/../'); -config.loadConfig(); + +(function (argv) { + var conf_switch = argv.indexOf('-c'); + if (conf_switch !== -1) { + if (argv[conf_switch + 1]) { + return config.loadConfig(argv[conf_switch + 1]); + } + } + + config.loadConfig(); + +})(process.argv); // If we're not running in the forground and we have a log file.. switch diff --git a/server/server.js b/server/server.js index a139f60..3399db0 100644 --- a/server/server.js +++ b/server/server.js @@ -49,5 +49,5 @@ switch (process.argv[2]) { break; default: - console.log('Usage: [-f|start|stop|restart|status|reconfig|build]'); -} \ No newline at end of file + console.log('Usage: [-f|start|stop|restart|status|reconfig|build [-c ]]'); +} -- 2.25.1