1 | #!/usr/bin/env node
|
2 |
|
3 | var repl = require('repl').start('aws-sdk> '),
|
4 | replEval = repl.eval,
|
5 | defaultOptions = {
|
6 | logger: process.stdout,
|
7 | region: process.env.AWS_REGION || 'us-east-1'
|
8 | };
|
9 |
|
10 |
|
11 | function customEval(cmd, context, filename, callback) {
|
12 | replEval(cmd, context, filename, function(err, value) {
|
13 | if (err) {
|
14 | callback(err, null);
|
15 | return;
|
16 | }
|
17 |
|
18 | function consoleDataExtraction(resp) {
|
19 | context.data = resp.data;
|
20 | context.error = resp.error;
|
21 | callback(resp.error, resp.data);
|
22 | }
|
23 |
|
24 | if (value && value.constructor === AWS.Request && !value.__hasBeenEval__) {
|
25 |
|
26 | try {
|
27 | value.__hasBeenEval__ = true;
|
28 | if (value.response) value.response.__hasBeenEval__ = true;
|
29 | context.request = value;
|
30 | context.response = value.response || null;
|
31 | context.data = null;
|
32 | context.error = null;
|
33 | if (value._asm.currentState === 'complete' && value.response) {
|
34 | context.data = value.response.data || null;
|
35 | context.error = value.response.error || null;
|
36 | callback(value.response.error, value.response.data);
|
37 | } else {
|
38 | value.on('complete', consoleDataExtraction);
|
39 | if (!value.__hasBeenSent__) {
|
40 | if (context.autoSend) {
|
41 | value.send();
|
42 | } else {
|
43 | callback(null, value);
|
44 | }
|
45 | }
|
46 | }
|
47 | } catch (err2) {
|
48 | callback(err2, null);
|
49 | return;
|
50 | }
|
51 |
|
52 | } else if (value && value.constructor === AWS.Response && !value.__hasBeenEval__) {
|
53 | try {
|
54 | value.__hasBeenEval__ = true;
|
55 | context.response = value;
|
56 | context.request = value.request || null;
|
57 | context.data = value.data || null;
|
58 | context.error = value.error || null;
|
59 | if (value.request) {
|
60 | value.request.__hasBeenEval__ = true;
|
61 | if (value.request._asm.currentState === 'complete') {
|
62 | callback(value.error, value.data);
|
63 | } else {
|
64 | value.request.on('complete', consoleDataExtraction);
|
65 | }
|
66 | }
|
67 | } catch (err2) {
|
68 | callback(err2, null);
|
69 | return;
|
70 | }
|
71 | } else {
|
72 | callback(null, value);
|
73 | }
|
74 | });
|
75 | }
|
76 |
|
77 | var AWS = repl.context.AWS = require('../lib/aws');
|
78 | repl.eval = customEval;
|
79 |
|
80 | // context variables
|
81 | repl.context.data = null;
|
82 | repl.context.error = null;
|
83 | repl.context.request = null;
|
84 | repl.context.response = null;
|
85 |
|
86 | // setup REPL history
|
87 | try {
|
88 | var replHistory = require('repl.history');
|
89 | replHistory(repl, process.env.HOME + '/.node_history');
|
90 | } catch (e) {
|
91 | console.log("Missing repl.history package, history will not be supported.");
|
92 | console.log(" Type `npm install repl.history` to enable history.");
|
93 | }
|
94 |
|
95 | // modify Request.prototype.send listener to track if the listener has been called
|
96 | var sendListener = AWS.Request.prototype.send;
|
97 | AWS.Request.prototype.send = function(callback) {
|
98 | this.__hasBeenSent__ = true;
|
99 | return sendListener.call(this, callback);
|
100 | };
|
101 |
|
102 | // flag to indicate that requests should be sent when callback is not provided
|
103 | // by default this is on, but can be turned off by setting `autoSend = false`
|
104 | repl.context.autoSend = true;
|
105 |
|
106 | // load services as defined instances
|
107 | for (var key in AWS) {
|
108 | var id = AWS[key].serviceIdentifier;
|
109 | if (id) {
|
110 | if (id === 'cloudsearchdomain' || id === 'iotdata') continue; // this required an explicit endpoint
|
111 |
|
112 | var svcClass = AWS[key];
|
113 | var svc = new svcClass(defaultOptions);
|
114 | svc.with = function(config) {
|
115 | return new this.constructor.__super__(AWS.util.merge(this.config, config));
|
116 | };
|
117 | repl.context[svcClass.serviceIdentifier] = svc;
|
118 | }
|
119 | }
|