From 50718c424f7d30b1a124e9e3328e24207f4fd71c Mon Sep 17 00:00:00 2001 From: coderaiser Date: Tue, 31 Mar 2015 04:46:07 -0400 Subject: [PATCH] feature(cloudcmd) add -c, --config --- HELP.md | 1 + bin/cloudcmd.js | 33 +++++++++++++++++++++++++++++++-- json/bin.json | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/HELP.md b/HELP.md index 05eccd9a..30022c62 100644 --- a/HELP.md +++ b/HELP.md @@ -64,6 +64,7 @@ Cloud Commander supports command line parameters: | `-a, --auth` | enable authorization | `-u, --username` | set username | `-p, --password` | set password +| `-c, --config` | configuration file path | `--port` | set port number | `--no-auth` | disable authorization | `--no-server` | do not start server diff --git a/bin/cloudcmd.js b/bin/cloudcmd.js index 6bf5f25a..3cf6470c 100755 --- a/bin/cloudcmd.js +++ b/bin/cloudcmd.js @@ -23,6 +23,7 @@ 'username', 'online', 'offline', + 'config' ], boolean: [ 'auth', @@ -44,7 +45,8 @@ o: 'online', u: 'username', s: 'save', - a: 'auth' + a: 'auth', + c: 'config' } }); @@ -63,6 +65,8 @@ config('online', args.online); config('username', args.username); + readConfig(args.config); + if (args.save) config.save(start); else @@ -97,7 +101,27 @@ if (!isNaN(number)) config('port', number); else - console.error('port: ignored, should be a number'); + exit('port should be a number'); + } + + function readConfig(name) { + var fs, data, error, tryCatch; + + if (name) { + fs = require('fs'); + tryCatch = require('try-catch'); + error = tryCatch(function() { + var json = fs.readFileSync(name); + data = JSON.parse(json); + }); + + if (error) + exit(error.message); + else + Object.keys(data).forEach(function(item) { + config(item, data[item]); + }); + } } function help() { @@ -124,4 +148,9 @@ require(DIR_LIB + '/server/repl'); } + function exit(message) { + console.error(message); + process.exit(1); + } + })(); diff --git a/json/bin.json b/json/bin.json index 0de30f63..6b4c7580 100644 --- a/json/bin.json +++ b/json/bin.json @@ -6,6 +6,7 @@ "-a, --auth " : "enable authorization", "-u, --username " : "set username", "-p, --password " : "set password", + "-c, --config " : "configuration file path", "--port " : "set port number", "--no-auth " : "disable authorization", "--no-server " : "do not start server",