MXS-1300: Refactor the way maxctrl is invoked

Making the invocation explicit should allow testing without actually
starting the process fromt the command line. Also returning a promise
allows chaining of commands for testing and verification.
This commit is contained in:
Markus Mäkelä
2017-07-12 16:53:53 +03:00
parent 6c601955d6
commit a082871726
2 changed files with 81 additions and 58 deletions

View File

@ -160,6 +160,9 @@ module.exports = function() {
})
})
})
.catch(function(err) {
logError(JSON.stringify(err, null, 4))
})
}
this.updateValue = function(resource, key, value) {

View File

@ -11,10 +11,24 @@
* Public License.
*/
require('./common.js')()
var fs = require('fs')
const maxctrl_version = '1.0.0';
module.exports = function(argv) {
// Mangle the arguments if we are being called from the command line
if (argv[0] == process.execPath) {
argv.shift()
}
try {
while (argv.length > 0) {
fs.accessSync(argv[0])
argv.shift()
}
} catch (err) { }
return new Promise(function(resolve, reject) {
program
.version(maxctrl_version)
.group(['u', 'p', 'h', 's', 't'], 'Global Options:')
@ -71,6 +85,12 @@ module.exports = function(argv) {
.command('*', 'the default command', {}, () => {
console.log('Unknown command. See output of `help` for a list of commands.')
})
.parse(argv)
.argv
.parse(process.argv, function(err, argv, output) {
if (err) {
reject(output)
} else {
resolve(output);
}
})
})
}