From 1c6064a4b240b4198afa277308328a184a7e5bbb Mon Sep 17 00:00:00 2001 From: Alexey Khristov Date: Tue, 30 Dec 2014 17:09:51 +0200 Subject: [PATCH] add --config option and read default pm2-gui.json file --- README.md | 19 +++++++++++++++--- bin/pm2-gui | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++- package.json | 2 +- 3 files changed, 70 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 546bfb3..2d8c885 100644 --- a/README.md +++ b/README.md @@ -76,15 +76,17 @@ $ npm install -g pm2-gui -h, --help output usage information --no-debug hide stdout/stderr information + --config path to custom .json config. Default value pm2-gui.json ``` ## Configs ```javascript { - "refresh": 3000 - "manipulation": true - "pm2": "~/.pm2" + "refresh": 3000, + "manipulation": true, + "pm2": "~/.pm2", + "port": 8088 } ``` @@ -92,6 +94,17 @@ $ npm install -g pm2-gui - **manupulation** A value indicates whether the client has permission to restart/stop processes, `true` by default. - **pm2** Root directory of Unitech/PM2, `~/.pm2` by default. - **password** The encrypted authentication code, if this config is set, users need to be authorized before accessing the index page. +- **port** Web GUI port, can be set only from config file + +### Config file +Config file can be set with option --config + +Example +```bash +$ pm2-gui start --config pm2-gui.json +``` + +If config name pm2-gui.json - option can be omitted ### Set Config Usage diff --git a/bin/pm2-gui b/bin/pm2-gui index 2b93160..efae12c 100755 --- a/bin/pm2-gui +++ b/bin/pm2-gui @@ -2,6 +2,7 @@ var commander = require('commander'), path = p = require('path'), + fs = require('fs'), chalk = require('chalk'), _ = require('lodash'), pkg = require('../package.json'), @@ -25,17 +26,68 @@ commander.on('-c', function(){ console.log(arguments); }) +var defaultConfigFileName = 'pm2-gui.json'; +var acceptKeys = ['pm2', 'refresh', 'manipulation', 'password']; // Web interface commander.command('start [port]') + .option('--config ', 'pass json config file with options') .option('--no-debug', 'hide stdout/stderr information') .description('Launch the web server, port default by 8088') .action(function(port, cmd){ + + function setupOptions(jsonConfig) { + var mon = Monitor(); + var passwordKeyName = acceptKeys[acceptKeys.length - 1]; + acceptKeys.forEach(function(keyName) { + var value = jsonConfig[keyName]; + if (!value) { + return; + } + if (keyName === passwordKeyName) { + var md5 = crypto.createHash('md5'); + md5.update(value); + value = md5.digest('hex'); + } + mon.config(keyName, value); + }); + } + + var configData; + if (cmd.config){ //if config file passed + + try { + configData = fs.readFileSync(cmd.config); + } catch (e) { + if (e.code === 'ENOENT') { + console.log('Config file %s not found', cmd.config); + process.exit(1); + } else { + console.log('Error read config file %s, error: %j', cmd.config, err); + process.exit(2); + } + } + } else { + try { + configData = fs.readFileSync(defaultConfigFileName); + } catch (e) {} + } + + try { + var jsonConfig = JSON.parse(configData); + setupOptions(jsonConfig); + } catch (e) { + console.log('Cant convert file %s to JSON', cmd.config); + process.exit(3); + } + + if (!port && jsonConfig.port) { // setup port from config file + port = jsonConfig.port; + } interface(port, cmd.debug); }); // Configuration -var acceptKeys = ['pm2', 'refresh', 'manipulation', 'password']; function showConfigs(cmd, mon){ if (!mon) { mon = Monitor(); diff --git a/package.json b/package.json index 432c5b8..48015e7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pm2-gui", - "version": "0.0.7", + "version": "0.0.8", "description": "An elegant web interface for Unitech/PM2.", "scripts": { },