diff --git a/packages/xen-api/README.md b/packages/xen-api/README.md index 7160b145c..a6088106a 100644 --- a/packages/xen-api/README.md +++ b/packages/xen-api/README.md @@ -12,6 +12,8 @@ Installation of the [npm package](https://npmjs.org/package/xen-api): ## Usage +### Library + ```javascript var createClient = require('xen-api').createClient @@ -34,6 +36,21 @@ xapi.objects.on('add', function (objects) { }) ``` +### CLI + +A CLI is provided to help exploration and discovery of the XAPI. + +``` +> xen-api https://xen1.company.net root +Password: ****** +root@xen1.company.net> xapi.connect +'connected' +root@xen1.company.net> xapi.pool.master.name_label +'xen1' +``` + + + ## Development ### Installing dependencies diff --git a/packages/xen-api/package.json b/packages/xen-api/package.json index 3a25a7572..d34ffdfe0 100644 --- a/packages/xen-api/package.json +++ b/packages/xen-api/package.json @@ -22,6 +22,9 @@ }, "preferGlobal": false, "main": "dist/", + "bin": { + "xen-api": "dist/cli.js" + }, "files": [ "dist/" ], @@ -29,12 +32,16 @@ "babel-runtime": "^5", "bluebird": "^2.9.21", "debug": "^2.1.3", + "event-to-promise": "^0.3.2", + "exec-promise": "^0.5.1", "lodash.find": "^3.2.0", "lodash.findkey": "^3.0.1", "lodash.foreach": "^3.0.2", "lodash.size": "^3.0.0", "lodash.startswith": "^3.0.1", "make-error": "^1.0.2", + "minimist": "^1.1.1", + "pw": "0.0.4", "xmlrpc": "^1.3.0", "xo-collection": "0.0.1" }, diff --git a/packages/xen-api/src/cli.js b/packages/xen-api/src/cli.js new file mode 100644 index 000000000..82f0eeecc --- /dev/null +++ b/packages/xen-api/src/cli.js @@ -0,0 +1,52 @@ +import Bluebird, {coroutine} from 'bluebird' +import eventToPromise from 'event-to-promise' +import execPromise from 'exec-promise' +import minimist from 'minimist' +import pw from 'pw' +import {start as createRepl} from 'repl' + +import {createClient} from './' + +const usage = `Usage: xen-api ` + +const main = coroutine(function * (args) { + const opts = minimist(args, { + boolean: ['help'], + + alias: { + help: 'h' + } + }) + + if (opts.help) { + return usage + } + + const [url, user] = opts._ + if (!url || !user) { + throw new Error('missing arguments') + } + + process.stdout.write('Password: ') + const password = yield new Bluebird(resolve => { + pw(resolve) + }) + + const xapi = createClient({url, auth: {user, password}}) + yield xapi.connect() + + const repl = createRepl({ + prompt: `${xapi._humanId}> ` + }) + repl.context.xapi = xapi + yield eventToPromise(repl, 'exit') + + try { + yield xapi.disconnect() + } catch (error) {} +}) +export default main + +if (!module.parent) { + execPromise(main) +}