Add find() and findAll() in CLI.

This commit is contained in:
Julien Fontanet 2015-10-23 14:51:25 +02:00
parent 799f758dce
commit 95989ff63b
3 changed files with 16 additions and 1 deletions

View File

@ -51,7 +51,9 @@ xapi.objects.on('add', function (objects) {
})
```
Custom fields on objects (hidden and read-only):
> Note: all objects are frozen and cannot be altered!
Custom fields on objects (hidden ie. non enumerable):
- `$type`: the type of the object (`VM`, `task`, …);
- `$ref`: the (opaque) reference of the object;
- `$id`: the identifier of this object (its UUID if any, otherwise its reference);
@ -80,6 +82,13 @@ root@xen1.company.net> xapi.pool.$master.name_label
'xen1'
```
To ease searches, `find()` and `findAll()` functions are available:
```
root@xen1.company.net> findAll({ $type: 'VM' }).length
183
```
## Development
### Installing dependencies

View File

@ -37,6 +37,7 @@
"exec-promise": "^0.5.1",
"kindof": "^2.0.0",
"lodash.filter": "^3.1.1",
"lodash.find": "^3.2.1",
"lodash.foreach": "^3.0.2",
"lodash.isarray": "^3.0.2",
"lodash.isobject": "^3.0.1",

View File

@ -4,6 +4,8 @@ import blocked from 'blocked'
import Bluebird, {coroutine} from 'bluebird'
import eventToPromise from 'event-to-promise'
import execPromise from 'exec-promise'
import filter from 'lodash.filter'
import find from 'lodash.find'
import minimist from 'minimist'
import pw from 'pw'
import {start as createRepl} from 'repl'
@ -84,6 +86,9 @@ const main = coroutine(function * (args) {
})
repl.context.xapi = xapi
repl.context.find = predicate => find(xapi.objects.all, predicate)
repl.context.findAll = predicate => filter(xapi.objects.all, predicate)
// Make the REPL waits for promise completion.
{
const evaluate = Bluebird.promisify(repl.eval)