From 2a2a2b72e9d8f52f79341a6d6c3d4bd12e14423e Mon Sep 17 00:00:00 2001 From: baldo Date: Sat, 11 Jun 2016 17:15:19 +0200 Subject: [PATCH] Get config.json location from command line parameter. --- package.json | 2 ++ server/config.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index af5f11c..81ebad1 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,8 @@ "dependencies": { "async": "~1.5.2", "body-parser": "~1.15.1", + "command-line-args": "~3.0.0", + "command-line-usage": "~3.0.1", "compression": "~1.6.2", "deep-extend": "~0.4.1", "express": "~4.13.4", diff --git a/server/config.js b/server/config.js index 847ab80..0bd65f4 100644 --- a/server/config.js +++ b/server/config.js @@ -1,5 +1,33 @@ 'use strict'; +var commandLineArgs = require('command-line-args'); +var commandLineUsage = require('command-line-usage'); + +var commandLineDefs = [ + { name: 'help', alias: 'h', type: Boolean, description: 'Show this help' }, + { name: 'config', alias: 'c', type: String, description: 'Location of config.json' } +]; + +var commandLineOptions; +try { + commandLineOptions = commandLineArgs(commandLineDefs); +} catch (e) { + console.error(e.message); + console.error('Try \'--help\' for more information.'); + process.exit(1); +} + +if (commandLineOptions.help || !commandLineOptions.config) { + console.log(commandLineUsage([ + { + header: 'ffffng - Freifunk node management form', + optionList: commandLineDefs + } + ])); + process.exit(1); +} + + var fs = require('fs'); var deepExtend = require('deep-extend'); @@ -65,11 +93,14 @@ var defaultConfig = { } }; -var configJSONFile = __dirname + '/../config.json'; +var configJSONFile = commandLineOptions.config; var configJSON = {}; if (fs.existsSync(configJSONFile)) { configJSON = JSON.parse(fs.readFileSync(configJSONFile, 'utf8')); +} else { + console.error('config.json not found: ' + configJSONFile); + process.exit(1); } var config = deepExtend({}, defaultConfig, configJSON);