From e2411f5cbb874393b7f5f3c28ae16a46293c5d3a Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Fri, 4 Sep 2015 14:49:26 -0400 Subject: [PATCH] make `triton cloudapi` more like curl - `-X method` to specify method - `-H 'key: value'` to specify header --- lib/do_cloudapi.js | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/do_cloudapi.js b/lib/do_cloudapi.js index f0cbcee..44c5e0b 100644 --- a/lib/do_cloudapi.js +++ b/lib/do_cloudapi.js @@ -16,23 +16,33 @@ function do_cloudapi(subcmd, opts, args, callback) { if (opts.help) { this.do_help('help', {}, [subcmd], callback); return; - } else if (args.length < 1 || args.length > 2) { + } else if (args.length < 1) { callback(new Error('invalid arguments')); return; } - var method = args[0]; - var path = args[1]; - if (path === undefined) { - path = method; - method = 'GET'; - } + var path = args[0]; var reqopts = { - method: method.toLowerCase(), + method: opts.method.toLowerCase(), + headers: {}, path: path }; + // parse -H headers + for (var i = 0; i < opts.header.length; i++) { + var raw = opts.header[i]; + var j = raw.indexOf(':'); + if (j < 0) { + callback(new Error('failed to parse header: ' + raw)); + return; + } + var header = raw.substr(0, j); + var value = raw.substr(j + 1).leftTrim(); + + reqopts.headers[header] = value; + } + this.tritonapi.cloudapi._request(reqopts, function (err, req, res, body) { if (err) { callback(err); @@ -62,6 +72,18 @@ do_cloudapi.options = [ type: 'bool', help: 'Show this help.' }, + { + names: ['method', 'X'], + type: 'string', + default: 'GET', + help: 'Request method to use. Default is "GET".' + }, + { + names: ['header', 'H'], + type: 'arrayOfString', + default: [], + help: 'Headers to send with request.' + }, { names: ['headers', 'i'], type: 'bool', @@ -72,7 +94,7 @@ do_cloudapi.help = ( 'Raw cloudapi request.\n' + '\n' + 'Usage:\n' - + ' {{name}} \n' + + ' {{name}} [-X method] [-H header=value] \n' + '\n' + '{{options}}' );