diff --git a/maxctrl/lib/common.js b/maxctrl/lib/common.js index bf786d6dc..83362b341 100644 --- a/maxctrl/lib/common.js +++ b/maxctrl/lib/common.js @@ -70,35 +70,53 @@ module.exports = function() { }) } + this.filterResource = function (res, fields) { + table = [] + + res.data.forEach(function(i) { + row = [] + + fields.forEach(function(p) { + var v = _.getPath(i, p[Object.keys(p)[0]], '') + + if (Array.isArray(v)) { + v = v.join(', ') + } + + row.push(v) + }) + + table.push(row) + }) + + return table + } + + // Get a resource as raw collection; a matrix of strings + this.getRawCollection = function (host, resource, fields) { + return getResource(host, resource) + .then((res) => filterResource(res, fields)) + } + + this.rawCollectionAsTable = function (arr, fields) { + var header = [] + + fields.forEach(function(i) { + header.push(Object.keys(i)) + }) + + var table = getTable(header) + + arr.forEach((row) => { + table.push(row) + }) + return table.toString() + } + // Request a resource collection and format it as a table this.getCollection = function (host, resource, fields) { - return doRequest(host, resource, function(res) { - - var header = [] - - fields.forEach(function(i) { - header.push(Object.keys(i)) - }) - - var table = getTable(header) - - res.data.forEach(function(i) { - row = [] - - fields.forEach(function(p) { - var v = _.getPath(i, p[Object.keys(p)[0]], '') - - if (Array.isArray(v)) { - v = v.join(', ') - } - row.push(v) - }) - - table.push(row) - }) - - return table.toString() - }) + return getRawCollection(host, resource, fields) + .then((res) => rawCollectionAsTable(res, fields)) } // Request a part of a resource as a collection @@ -250,6 +268,12 @@ module.exports = function() { return doAsyncRequest(host, resource, cb, obj) } + this.getResource = function(host, resource) { + return doAsyncRequest(host, resource, (res) => { + return res + }) + } + this.error = function(err) { return Promise.reject(colors.red('Error: ') + err) }