-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bin.js
executable file
·146 lines (124 loc) · 3.23 KB
/
bin.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
#!/usr/bin/env node
var registry = require('./');
var optimist = require('optimist');
var path = require('path');
var chalk = require('chalk');
var freeport = require('freeport');
var tree = require('pretty-tree');
var net = require('net');
var address = require('network-address');
var argv = optimist
.usage('Usage: $0 [command] [options]')
.option('e', {
alias: 'etcd',
default: '127.0.0.1:4001'
})
.option('v', {
alias: 'verbose',
default: false
})
.option('p', {
alias: 'port',
default: 0
})
.option('h', {
alias: 'hostname',
default: address()
})
.option('w', {
alias: 'wait',
default: 2000
})
.option('s', {
alias: 'slack',
default: 5000
})
.argv;
var services = registry(argv.etcd + (argv.etcd.indexOf('?') === -1 ? '?refresh=false' : '&refresh=false'));
var cmds = {};
var _ = argv._;
var help = function() {
optimist.showHelp();
console.error('Commands:')
console.error(' join [name] [index.js] # Listen on env.PORT to join the registry in index.js');
console.error(' list # List all added services');
console.error(' lookup [name] # Lookup a specific service');
console.error('');
process.exit(1);
};
var error = function(err) {
console.error(chalk.red(err.message || err));
process.exit(2);
};
var usage = function(msg) {
console.error('Usage: '+argv.$0+' '+msg);
process.exit(2);
};
cmds.join = function(name, main) {
if (!name || !main) return usage('join [name] [main]');
var onport = function(port) {
process.env.PORT = ''+port;
var join = function() {
services.join(name, {port:port, hostname:argv.hostname}, function(err) {
if (err) return error(err);
var pexit = process.exit;
process.exit = function(code) {
services.leave(function() {
pexit(code);
});
};
process.on('SIGTERM', function() {
var leave = function() {
services.leave(function() {
if (!argv.slack) return pexit();
setTimeout(pexit, parseInt(argv.slack, 10));
});
};
if (!argv.wait) return leave();
setTimeout(leave, parseInt(argv.wait, 10));
});
process.on('SIGINT', function() {
process.exit();
});
});
};
var listen = net.Server.prototype.listen;
net.Server.prototype.listen = function(p) {
if (Number(p) === Number(port)) this.once('listening', join);
listen.apply(this, arguments);
};
require(path.join(process.cwd(), main));
};
if (argv.port) return onport(parseInt(argv.port));
freeport(function(err, port) {
if (err) return error(err);
onport(port);
});
};
var onservice = function(service) {
var name = service.name;
delete service.name
console.log(tree({
label: name,
leaf: service
}));
};
cmds.list = function() {
services.list(function(err, list) {
if (err) return error(err);
if (!list.length) return console.log(chalk.grey('(empty)'));
list.forEach(function(service) {
if (argv.verbose) return onservice(service);
console.log(chalk.yellow(service.name));
});
});
};
cmds.lookup = function(name) {
if (!name) return usage('lookup [name]');
services.lookup(name, function(err, service) {
if (err) return error(err);
if (!service) return console.log(chalk.grey('(empty)'));
onservice(service);
});
};
(cmds[_[0]] || help).apply(null, _.slice(1));