diff --git a/HELP.md b/HELP.md index d2311e98..ae5b34c6 100644 --- a/HELP.md +++ b/HELP.md @@ -71,6 +71,7 @@ Cloud Commander supports command line parameters: | `-u, --username` | set username | `-p, --password` | set password | `-c, --config` | configuration file path +| `--show-config` | show config values | `--editor` | set editor: "dword", "edward" or "deepword" | `--packer` | set packer: "tar" or "zip" | `--root` | set root directory diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index 9910bcf9..c47ef39d 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -42,7 +42,8 @@ const args = require('minimist')(argv.slice(2), { 'console', 'terminal', 'one-panel-mode', - 'html-dialogs' + 'html-dialogs', + 'show-config', ], default: { server : true, @@ -128,6 +129,9 @@ function main() { validateRoot(options.root); + if (args['show-config']) + showConfig(); + if (!args.save) return start(options); @@ -167,6 +171,13 @@ function port(arg) { exit('cloudcmd --port: should be a number'); } +function showConfig() { + const show = require('../server/show-config'); + const data = show(config('*')); + + console.log(data); +} + function readConfig(name) { if (!name) return; diff --git a/json/config.json b/json/config.json index 38638b36..4981a102 100644 --- a/json/config.json +++ b/json/config.json @@ -26,6 +26,7 @@ "configDialog": true, "console": true, "terminal": false, - "terminalPath": "" + "terminalPath": "", + "showConfig": "false" } diff --git a/json/help.json b/json/help.json index cbb6e27b..20b5ba54 100644 --- a/json/help.json +++ b/json/help.json @@ -7,6 +7,7 @@ "-u, --username ": "set username", "-p, --password ": "set password", "-c, --config ": "configuration file path", + "--show-config ": "show config values", "--editor ": "set editor: \"dword\", \"edward\" or \"deepword\"", "--packer ": "set packer: \"tar\" or \"zip\"", "--root ": "set root directory", diff --git a/man/cloudcmd.1 b/man/cloudcmd.1 index 0cceb1d9..2841a05f 100644 --- a/man/cloudcmd.1 +++ b/man/cloudcmd.1 @@ -30,6 +30,7 @@ programs in browser from any computer, mobile or tablet device. -u, --username set username -p, --password set password -c, --config configuration file path + --show-config show config values --editor set editor: "dword", "edward" or "deepword" --packer set packer: "tar" or "zip" --root set root directory diff --git a/package.json b/package.json index 29103b25..585e3863 100644 --- a/package.json +++ b/package.json @@ -154,6 +154,7 @@ "socket.io": "^2.0.1", "spero": "^1.5.0", "squad": "^1.1.3", + "table": "^4.0.1", "try-catch": "^1.0.0", "tryrequire": "^1.1.5", "writejson": "^1.1.0" diff --git a/server/show-config.js b/server/show-config.js new file mode 100644 index 00000000..b9902b03 --- /dev/null +++ b/server/show-config.js @@ -0,0 +1,35 @@ +'use strict'; + +const t = require('table'); +const table = t.table; +const getBorderCharacters = t.getBorderCharacters; + +module.exports = (config) => { + check(config); + + const data = Object.keys(config).map((name) => { + return [name, config[name]]; + }); + + if (!data.length) + return ''; + + return table(data, { + columns: { + 1: { + width: 30, + truncate: 30, + } + }, + border: getBorderCharacters('ramac'), + }); +}; + +function check(config) { + if (!config) + throw Error('config could not be empty!'); + + if (typeof config !== 'object') + throw Error('config should be an object!'); +} + diff --git a/test/server/show-config.js b/test/server/show-config.js new file mode 100644 index 00000000..d2d597db --- /dev/null +++ b/test/server/show-config.js @@ -0,0 +1,36 @@ +'use strict'; + +const test = require('tape'); +const showConfig = require('../../server/show-config'); + +test('cloudcmd: show-config: no arguments', (t) => { + t.throws(showConfig, /config could not be empty!/, 'should throw when no config'); + t.end(); +}); + +test('cloudcmd: show-config: bad arguments', (t) => { + const fn = () => showConfig('hello'); + t.throws(fn, /config should be an object!/, 'should throw when config not object'); + t.end(); +}); + +test('cloudcmd: show-config: return', (t) => { + t.equal(showConfig({}), '', 'should return string'); + t.end(); +}); + +test('cloudcmd: show-config: return', (t) => { + const config = { + hello: 'world' + }; + + const result = [ + '+-------+--------------------------------+\n', + '| hello | world |\n', + '+-------+--------------------------------+\n', + ].join(''); + + t.equal(showConfig(config), result, 'should return table'); + t.end(); +}); +