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:
Markus Mäkelä 2017-07-02 09:30:56 +03:00
parent c7b9b7ac4a
commit cfa0fd9803

View File

@ -15,7 +15,6 @@ var _ = require('lodash-getpath')
var request = require('request');
var colors = require('colors/safe');
var Table = require('cli-table');
var assert = require('assert')
module.exports = function() {
@ -103,7 +102,7 @@ module.exports = function() {
request(args, function(err, resp, res) {
if (err) {
// Failed to request
console.log('Error:', JSON.stringify(err, null, 4))
logError(JSON.stringify(err, null, 4))
} else if (resp.statusCode == 200 && cb) {
// Request OK, returns data
cb(res)
@ -112,13 +111,24 @@ module.exports = function() {
console.log(colors.green('OK'))
} else {
// Unexpected return code, probably an error
console.log('Error:', resp.statusCode, resp.statusMessage)
var errstr = resp.statusCode + ' ' + resp.statusMessage
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() {