Permalink
Browse files

service and query for the command line tool

  • Loading branch information...
1 parent fb48c93 commit 1a17eaeff8c78f5d2615d853eec30e805e4b6955 @substack committed Feb 9, 2012
Showing with 43 additions and 4 deletions.
  1. +43 −4 bin/cli.js
View
@@ -1,12 +1,30 @@
#!/usr/bin/env node
var seaport = require('../');
+var spawn = require('child_process').spawn;
var argv = require('optimist')
.demand(1)
.usage([
'Usage:',
- ' $0 port # to listen',
- ' $0 host:port show # to show the port map',
+ '',
+ ' $0 port',
+ '',
+ ' Create seaport server.',
+ '',
+ ' $0 host:port show',
+ '',
+ ' Show the port map from the server at host:port.',
+ '',
+ ' $0 host:port service name@version [COMMAND...]',
+ '',
+ ' Register a service. COMMAND will get an assigned port to use as'
+ + ' its last argument. If COMMAND exits it will be restarted.',
+ '',
+ ' $0 host:port query name@version',
+ '',
+ ' Query the server for services matching the name@version pattern.',
+ ' The version may contain semver patterns to specify a range.',
+ ' Prints out a JSON array of host:port strings.',
].join('\r\n'))
.argv
;
@@ -25,8 +43,29 @@ else {
var cmd = argv._[1];
- if (cmd === 'show' || cmd === undefined) {
- var ports = seaport().connect(host, port);
+ if (cmd === 'service') {
+ var ports = seaport.connect(host, port);
+ ports.service(argv._[2], function respawn (port) {
+ var ps = spawn(argv._[3], argv._.slice(4).concat(port));
+ ps.stdout.pipe(process.stdout, { end : false });
+ ps.stderr.pipe(process.stderr, { end : false });
+
+ ps.on('exit', function () {
+ setTimeout(respawn, 1000, port);
+ });
+ });
+ }
+ else if (cmd === 'query') {
+ var ports = seaport.connect(host, port);
+ ports.query(argv._[2], function (ps) {
+ console.log(JSON.stringify(ps.map(function (p) {
+ return p.host + ':' + p.port
+ })));
+ ports.close();
+ });
+ }
+ else if (cmd === 'show' || cmd === undefined) {
+ var ports = seaport.connect(host, port);
ports.query(function (ps) {
console.log(JSON.stringify(ps, undefined, 2));
ports.close();

0 comments on commit 1a17eae

Please sign in to comment.