Add ability to specify configuration file on the command line
authorJack Allnutt <jack@allnutt.eu>
Tue, 4 Feb 2014 15:30:19 +0000 (15:30 +0000)
committerJack Allnutt <jack@allnutt.eu>
Tue, 4 Feb 2014 15:33:34 +0000 (15:33 +0000)
Resolves #105

server/configuration.js
server/kiwi.js
server/server.js

index bc05e6ebd669adb00dda21f3dafa184d86fac109..b8241f33f46731c73195766c76aa10f60f5b4df6 100644 (file)
@@ -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;
         }
     }
 
index cd2515579a4605da571c94657617fb90ade4d9c6..44c267eae1cf5b7b661be6e3496048d93df5665d 100755 (executable)
@@ -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
index a139f60ce78d168c81d1d6a3471f7049656e98df..3399db0def8914a5ecfc03276d660de2a5adca1b 100644 (file)
@@ -49,5 +49,5 @@ switch (process.argv[2]) {
         break;\r
         \r
     default:\r
-        console.log('Usage: [-f|start|stop|restart|status|reconfig|build]');\r
-}
\ No newline at end of file
+        console.log('Usage: [-f|start|stop|restart|status|reconfig|build [-c <config file>]]');\r
+}\r