feat(list objects): ability to select displayed properties

Fixes #18
This commit is contained in:
Julien Fontanet 2016-11-24 12:33:24 +01:00
parent 731e2dc4c4
commit a67b6130f8

View File

@ -17,6 +17,7 @@ var forEach = require('lodash/forEach')
var getKeys = require('lodash/keys') var getKeys = require('lodash/keys')
var got = require('got') var got = require('got')
var humanFormat = require('human-format') var humanFormat = require('human-format')
var identity = require('lodash/identity')
var isObject = require('lodash/isObject') var isObject = require('lodash/isObject')
var micromatch = require('micromatch') var micromatch = require('micromatch')
var multiline = require('multiline') var multiline = require('multiline')
@ -57,6 +58,24 @@ function _startsWith (string, search) {
return string.lastIndexOf(search, 0) === 0 return string.lastIndexOf(search, 0) === 0
} }
var FLAG_RE = /^--([^=]+)(?:=([^]*))?$/
function extractFlags (args) {
var flags = {}
var i = 0
var n = args.length
var matches
while (i < n && (matches = args[i].match(FLAG_RE))) {
var value = matches[2]
flags[matches[1]] = value === undefined ? true : value
++i
}
args.splice(0, i)
return flags
}
var PARAM_RE = /^([^=]+)=([^]*)$/ var PARAM_RE = /^([^=]+)=([^]*)$/
function parseParameters (args) { function parseParameters (args) {
var params = {} var params = {}
@ -133,9 +152,15 @@ var help = wrap((function (pkg) {
The patterns can be used to filter on command names. The patterns can be used to filter on command names.
$name --list-objects [<property>=<value>]... $name --list-objects [--<property>] [<property>=<value>]...
Returns a list of XO objects. Returns a list of XO objects.
--<property>
Restricts displayed properties to those listed.
<property>=<value>
Restricted displayed objects to those matching the patterns.
$name <command> [<name>=<value>]... $name <command> [<name>=<value>]...
Executes a command on the current XO instance. Executes a command on the current XO instance.
@ -265,9 +290,14 @@ function listCommands (args) {
exports.listCommands = listCommands exports.listCommands = listCommands
function listObjects (args) { function listObjects (args) {
var sieve = args.length var properties = getKeys(extractFlags(args))
? parseParameters(args) var filterProperties = properties.length
: null ? function (object) {
return pick(object, properties)
}
: identity
var sieve = args.length && parseParameters(args)
return connect().then(function getXoObjects (xo) { return connect().then(function getXoObjects (xo) {
return xo.call('xo.getAllObjects') return xo.call('xo.getAllObjects')
@ -277,7 +307,7 @@ function listObjects (args) {
const stdout = process.stdout const stdout = process.stdout
stdout.write('[\n') stdout.write('[\n')
for (var i = 0, n = objects.length; i < n;) { for (var i = 0, n = objects.length; i < n;) {
stdout.write(JSON.stringify(objects[i], null, 2)) stdout.write(JSON.stringify(filterProperties(objects[i]), null, 2))
stdout.write(++i < n ? ',\n' : '\n') stdout.write(++i < n ? ',\n' : '\n')
} }
stdout.write(']') stdout.write(']')