Permalink
Browse files

Convert to JavaScript.

Checking in the CoffeeScript generated JavaScript. Going to move forward
using the JavaScript as the source.
  • Loading branch information...
1 parent 9a261f7 commit f9e13eebd10668131833e5465c46ecfed4677b71 @bigeasy bigeasy committed Jun 8, 2012
Showing with 363 additions and 338 deletions.
  1. +99 −0 bin/ec2
  2. +75 −0 lib/amazon.js
  3. +97 −0 lib/request.js
  4. +92 −0 lib/response.js
  5. +0 −77 src/bin/ec2.coffee
  6. +0 −49 src/lib/amazon.coffee
  7. +0 −88 src/lib/request.coffee
  8. +0 −124 src/lib/response.coffee
View
99 bin/ec2
@@ -0,0 +1,99 @@
+#!/usr/bin/env node
+(function() {
+ var argv, build, client, command, configuration, display, ec2, file, format, fs, key, labels, match, parameters, parse, rest, _ref;
+ fs = require("fs");
+ ec2 = require("ec2");
+ argv = process.argv.slice(2);
+ command = argv.shift();
+ parameters = {};
+ while (argv.length) {
+ key = argv.shift();
+ if (match = /^\s*\+(.*)/.exec(key)) {
+ format = match[1];
+ } else {
+ parameters[key] = argv.shift();
+ }
+ }
+ build = function(fields, child) {
+ return function(lines, line, context) {
+ var copy, field, item, n, _i, _len, _len2, _results;
+ _results = [];
+ for (_i = 0, _len = context.length; _i < _len; _i++) {
+ item = context[_i];
+ copy = line.slice(0);
+ for (n = 0, _len2 = fields.length; n < _len2; n++) {
+ field = fields[n];
+ if (child && n === fields.length - 1) {
+ child(lines, copy, item[field]);
+ } else {
+ copy.push(item[field]);
+ }
+ }
+ _results.push(!child ? lines.push(copy) : void 0);
+ }
+ return _results;
+ };
+ };
+ parse = function(labels, rest, nested) {
+ var child, field, fields, label, proc, _ref, _ref2, _ref3;
+ fields = [];
+ while (!proc) {
+ if (nested && (match = /^\s*\](.*)$/.exec(rest))) {
+ rest = match[1];
+ proc = build(fields, null);
+ } else {
+ match = /^\s*(\w[\w\d]*)(.*)$/.exec(rest);
+ if (!match) {
+ throw new Error("invalid pattern");
+ }
+ _ref = match.slice(1), field = _ref[0], rest = _ref[1];
+ label = field;
+ if (match = /^\[(.*)$/.exec(rest)) {
+ fields.push(field);
+ _ref2 = parse(labels, match[1], true), child = _ref2[0], rest = _ref2[1];
+ proc = build(fields, child);
+ } else {
+ if (match = /^\/(\w[\w\d]*)$/.exec(rest)) {
+ _ref3 = match.slice(1), label = _ref3[0], rest = _ref3[1];
+ }
+ labels.push(label);
+ fields.push(field);
+ rest = rest.replace(/^\s*,/, '');
+ }
+ }
+ }
+ return [proc, rest];
+ };
+ labels = [];
+ if (format) {
+ _ref = parse(labels, format), display = _ref[0], rest = _ref[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"));
+ client = ec2.createClient(configuration);
+ client.on("error", function(error) {
+ console.log("error", error);
+ throw error;
+ });
+ client.call(command, parameters, function(response) {
+ var line, lines, _i, _len, _results;
+ if (display) {
+ lines = [];
+ display(lines, [], [response]);
+ _results = [];
+ for (_i = 0, _len = lines.length; _i < _len; _i++) {
+ line = lines[_i];
+ process.stdout.write(line.join(" "));
+ _results.push(process.stdout.write("\n"));
+ }
+ return _results;
+ } else {
+ process.stdout.write(JSON.stringify(response, null, 2));
+ return process.stdout.write("\n");
+ }
+ });
+ client.execute();
+}).call(this);
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -0,0 +1,92 @@
+(function() {
+ var ResponseParser, isTimestamp, xml;
+
+ xml = require("node-xml");
+
+ isTimestamp = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d*)Z/;
+
+ ResponseParser = (function() {
+
+ function ResponseParser() {
+ var accumulator, depth, names,
+ _this = this;
+ accumulator = [];
+ depth = 0;
+ names = [];
+ this.sax = new xml.SaxParser(function(sax) {
+ sax.onStartElementNS(function(elem) {
+ if (/^item|Error$/.test(elem)) {
+ if (_this.branches.length !== depth) _this.branches.push([]);
+ names.push(elem);
+ } else if (depth !== 0) {
+ if (depth > 1 && _this.branches.length !== depth) {
+ _this.branches.push({});
+ }
+ names.push(elem);
+ }
+ accumulator.length = 0;
+ return depth++;
+ });
+ sax.onEndElementNS(function(elem) {
+ var map, match, name, value;
+ if (--depth === 0) {
+ return _this.callback(null, _this.base);
+ } else if (/^item|Error$/.test(names[depth - 1])) {
+ map = _this.branches.pop();
+ _this.branches[_this.branches.length - 1].push(map);
+ return names.pop();
+ } else if (_this.branches[depth]) {
+ map = _this.branches.pop();
+ return _this.branches[_this.branches.length - 1][names.pop()] = map;
+ } else {
+ name = names.pop();
+ if (accumulator.length === 0) {
+ value = null;
+ if (name === "Errors" || /Set$/.test(name)) value = [];
+ } else {
+ value = accumulator.join("");
+ if (/Time$/.test(name)) {
+ match = isTimestamp.exec(value);
+ if (match) {
+ match = match.slice(1).map(function(part) {
+ return parseInt(part, 10);
+ });
+ match[1]--;
+ value = new Date(Date.UTC.apply(null, match));
+ }
+ }
+ if (/size$/i.test(name)) value = parseInt(value, 10);
+ if (name === "return" && /^true|false$/.test(value)) {
+ value = value === "true";
+ }
+ }
+ _this.branches[_this.branches.length - 1][name] = value;
+ return accumulator.length = 0;
+ }
+ });
+ sax.onCharacters(function(chars) {
+ if (accumulator.length === 0 && /\S/.test(chars)) {
+ return accumulator.push(chars);
+ }
+ });
+ return sax.onError(function(msg) {
+ console.log("blurgh", msg);
+ return _this.callback(new Error(msg), null);
+ });
+ });
+ }
+
+ ResponseParser.prototype.read = function(text, callback) {
+ this.base = {};
+ this.branches = [this.base];
+ this.callback = callback || function() {};
+ return this.sax.parseString(text);
+ };
+
+ return ResponseParser;
+
+ })();
+
+ module.exports.ResponseParser = ResponseParser;
+
+}).call(this);
Oops, something went wrong.

0 comments on commit f9e13ee

Please sign in to comment.