make triton cloudapi more like curl

- `-X method` to specify method
- `-H 'key: value'` to specify header
This commit is contained in:
Dave Eddy 2015-09-04 14:49:26 -04:00
parent d8a5f247f2
commit e2411f5cbb

View File

@ -16,23 +16,33 @@ function do_cloudapi(subcmd, opts, args, callback) {
if (opts.help) { if (opts.help) {
this.do_help('help', {}, [subcmd], callback); this.do_help('help', {}, [subcmd], callback);
return; return;
} else if (args.length < 1 || args.length > 2) { } else if (args.length < 1) {
callback(new Error('invalid arguments')); callback(new Error('invalid arguments'));
return; return;
} }
var method = args[0]; var path = args[0];
var path = args[1];
if (path === undefined) {
path = method;
method = 'GET';
}
var reqopts = { var reqopts = {
method: method.toLowerCase(), method: opts.method.toLowerCase(),
headers: {},
path: path 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) { this.tritonapi.cloudapi._request(reqopts, function (err, req, res, body) {
if (err) { if (err) {
callback(err); callback(err);
@ -62,6 +72,18 @@ do_cloudapi.options = [
type: 'bool', type: 'bool',
help: 'Show this help.' 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'], names: ['headers', 'i'],
type: 'bool', type: 'bool',
@ -72,7 +94,7 @@ do_cloudapi.help = (
'Raw cloudapi request.\n' 'Raw cloudapi request.\n'
+ '\n' + '\n'
+ 'Usage:\n' + 'Usage:\n'
+ ' {{name}} <method> <endpoint>\n' + ' {{name}} [-X method] [-H header=value] <endpoint>\n'
+ '\n' + '\n'
+ '{{options}}' + '{{options}}'
); );