-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
184 lines (143 loc) · 4.04 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
var cli = {};
module['exports'] = cli;
cli.run = function (stdin, stdout, opts, cb){
var argv = opts.argv;
var request = require('hyperquest');
//var through = require("through2");
var colors = require('colors');
var config = require('./config');
var debug = console.log;
// TODO: add ability to signin and list your own hooks
// TODO: add ability to configure username
// TODO: make services list enumerable from live site
var services = {
"echo": {},
"coin": {},
"colors": {},
"image": {},
"geoip": {},
"markdown": {},
"spellcheck": {}
};
var commands = {
"init": {
"description": "creates a new Hook scaffold"
},
"push": {
"description": "pushes current Hook to hook.io"
},
"pull": {
"description": "pulls a local copy of a hook.io Hook"
},
"run": {
"description": "runs current Hook"
},
"test": {
"description": "runs tests for current Hook"
}
};
cli.commands = {};
for (var c in commands) {
cli.commands[c] = require('./lib/commands/' + c);
}
// If no microservice is provided, show a list of default microservices
/*
if (argv._.length === 0) {
console.log('No service selected.');
console.log(Object.keys(services).join('\n'));
}
*/
var command = argv._[0];
if (argv._.length === 0 ) {
console.log('no command has been selected'.red);
console.log('available commands:'.blue);
for (var c in commands) {
console.log(c.magenta + ' - '.grey + commands[c].description.grey)
}
//console.log(Object.keys(commands).join('\n'));
return;
return cb(null);
}
cli.commands[command]({}, function (err) {
// console.log(err, 'ran')
});
return;
// If a microservice is provided, attempt to run it
var data = {};
for(var p in argv) {
if (p === "_") {
continue;
}
data[p] = argv[p];
}
// If a second parameter has been included, assume its the first schema parameter
if (typeof argv._[1] !== 'undefined') {
// TODO
}
var base = config.endpoint,
hookName = '';
// check against top level hooks
if (typeof services[service] !== 'undefined') {
// found a top level hook service, use special url
base = "http://" + service + ".hook.io/";
hookName = "";
} else {
service = "/Marak/echo";
base = base + service;
}
// STDIN / REQUEST OUT stream processing
stdin.setEncoding('utf8');
var url = base + hookName;
// console.log('opening stream to ', url);
if (stdin.isTTY) {
// handle shell arguments
debug('making outgoing request', url);
var stream = request.post(url, { headers: { "Content-Type": "application/json" }});
// If the service is not found or an error occurs, show a friendly error
stream.on('error', function(e){
throw e;
});
stream.on('end', function(){
// console.log('stream ended');
});
stream.on('response', function(res){
if (res.statusCode === 404) {
console.log('Hook not found');
process.exit();
}
});
stream.write(JSON.stringify(data));
stream.end();
stream.on('data', function(d){
console.log(d.toString());
});
} else {
// handle piped content
debug('making outgoing request', url);
var stream = request.post(url, { headers: { "Content-Type": "application/octet-stream" }});
// TODO: If the service is not found or an error occurs, show a friendly error
stream.on('error', function(e){
throw e;
});
stream.on('end', function(){
console.log('stream ended');
});
stdin.on('readable', function() {
console.log("READING".red)
var chunk = stdin.read();
if (chunk !== null) {
stream.write(chunk);
}
});
stdin.on('end', function() {
console.log('ended event stdin')
// check if any data was recevived via stdin, if not, send json payload
//stdout.write('end');
stream.end();
});
stream.on('data', function(d){
console.log(d.toString());
});
}
cb(null);
};