Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 831c140883
Fetching contributors…

Cannot retrieve contributors at this time

executable file 101 lines (90 sloc) 2.589 kb
#!/usr/bin/env node
var configuration, display, file, format, labels, $, rest, _ref;
var fs = require("fs")
, ec2 = require("ec2")
, argv = process.argv.slice(2)
, command = argv.shift()
, parameters = {}
, key
;
while (argv.length) {
key = argv.shift();
if ($ = /^\s*\+(.*)/.exec(key)) format = $[1];
else parameters[key] = argv.shift();
}
function build (fields, child) {
return function(lines, line, context) {
var copy, field, item, i, I, j, J, results = [];
for (i = 0, I = context.length; i < I; i++) {
item = context[i];
copy = line.slice(0);
for (j = 0, J = fields.length; j < J; j++) {
field = fields[j];
if (child && j === fields.length - 1) {
child(lines, copy, item[field]);
} else {
copy.push(item[field]);
}
}
results.push(!child ? lines.push(copy) : void 0);
}
return results;
};
}
function parse (labels, rest, nested) {
var child, field, fields = [], label, proc;
while (!proc) {
if (nested && ($ = /^\s*\](.*)$/.exec(rest))) {
rest = $[1];
proc = build(fields, null);
} else {
$ = /^\s*(\w[\w\d]*)(.*)$/.exec(rest);
if (!$) {
throw new Error("invalid pattern");
}
field = $[1], rest = $[2];
label = field;
if ($ = /^\[(.*)$/.exec(rest)) {
fields.push(field);
$ = parse(labels, $[1], true), child = $[0], rest = $[1];
proc = build(fields, child);
} else {
if ($ = /^\/(\w[\w\d]*)(.*)$/.exec(rest)) {
label = $[1], rest = $[2];
}
labels.push(label);
fields.push(field);
rest = rest.replace(/^\s*,/, '');
if (!rest) proc = build(fields, null);
}
}
}
return [proc, rest];
}
labels = [];
if (format) {
$ = parse(labels, format), display = $[0], rest = $[1];
if (rest && rest.trim().length !== 0) {
throw new Error("invalid pattern.");
}
}
file = process.env["AWS_CONFIG"] || ("" + process.env["HOME"] + "/.aws");
configuration = JSON.parse(fs.readFileSync(file, "utf8"));
ec2 = ec2(configuration);
ec2(command, parameters, response);
function response (error, response) {
var line, lines, i, I, results;
if (error) throw error;
if (display) {
lines = [];
display(lines, [], [response]);
for (i = 0, I = lines.length; i < I; i++) {
line = lines[i];
process.stdout.write(line.join(" ") + "\n");
}
return results;
} else {
process.stdout.write(JSON.stringify(response, null, 2));
return process.stdout.write("\n");
}
}
Jump to Line
Something went wrong with that request. Please try again.