From cfa0fd980333eab13cf6e69ec620a601e6311168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Sun, 2 Jul 2017 09:30:56 +0300 Subject: [PATCH] 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. --- client/maxctrl/common.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/client/maxctrl/common.js b/client/maxctrl/common.js index 85a0833ca..f0a903ff6 100644 --- a/client/maxctrl/common.js +++ b/client/maxctrl/common.js @@ -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() {