MXS-1300: Create more helper functions
The common error logging function prints and colors the output in an uniform way. The updateValue function allows easy PATCH updates to a single value of a resource.
This commit is contained in:
@ -15,7 +15,6 @@ var _ = require('lodash-getpath')
|
|||||||
var request = require('request');
|
var request = require('request');
|
||||||
var colors = require('colors/safe');
|
var colors = require('colors/safe');
|
||||||
var Table = require('cli-table');
|
var Table = require('cli-table');
|
||||||
var assert = require('assert')
|
|
||||||
|
|
||||||
module.exports = function() {
|
module.exports = function() {
|
||||||
|
|
||||||
@ -103,7 +102,7 @@ module.exports = function() {
|
|||||||
request(args, function(err, resp, res) {
|
request(args, function(err, resp, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
// Failed to request
|
// Failed to request
|
||||||
console.log('Error:', JSON.stringify(err, null, 4))
|
logError(JSON.stringify(err, null, 4))
|
||||||
} else if (resp.statusCode == 200 && cb) {
|
} else if (resp.statusCode == 200 && cb) {
|
||||||
// Request OK, returns data
|
// Request OK, returns data
|
||||||
cb(res)
|
cb(res)
|
||||||
@ -112,13 +111,24 @@ module.exports = function() {
|
|||||||
console.log(colors.green('OK'))
|
console.log(colors.green('OK'))
|
||||||
} else {
|
} else {
|
||||||
// Unexpected return code, probably an error
|
// Unexpected return code, probably an error
|
||||||
console.log('Error:', resp.statusCode, resp.statusMessage)
|
var errstr = resp.statusCode + ' ' + resp.statusMessage
|
||||||
if (res) {
|
if (res) {
|
||||||
console.log(res)
|
errstr += ' ' + JSON.stringify(res, null, 4)
|
||||||
}
|
}
|
||||||
|
logError(errstr)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.updateValue = function(resource, key, value) {
|
||||||
|
var body = {}
|
||||||
|
_.set(body, key, value)
|
||||||
|
doRequest(resource, null, { method: 'PATCH', body: body })
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logError = function(err) {
|
||||||
|
console.log(colors.red('Error:'), err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getList() {
|
function getList() {
|
||||||
|
Reference in New Issue
Block a user