Skip to content

Commit

Permalink
[refactor] Refactor Forever service CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalecki authored and indexzero committed Dec 23, 2011
1 parent c01abef commit 079137c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 186 deletions.
7 changes: 1 addition & 6 deletions bin/foreverd
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
#!/usr/bin/env node

var Clip = require('clip');
require('../lib/forever/service/cli').startCLI();

var app = new Clip();

require('../lib/forever/service/cli')(app);

app.run();
242 changes: 62 additions & 180 deletions lib/forever/service/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,205 +7,87 @@
*/

var utile = require('utile'),
flatiron = require('flatiron'),
optimist = require('optimist'),
forever = require('../../forever'),
Service = require('./service'),
argv;

var mappings = {
'c': 'command',
'e': 'errFile',
'l': 'logFile',
'a': 'appendLog',
'append': 'appendLog',
'm': 'max',
'o': 'outFile',
'p': 'path',
'pidfile': 'pidFile',
's': 'silent',
'silent': 'silent',
'sourceDir': 'sourceDir',
'minUptime': 'minUptime',
'spinSleepTime': 'spinSleepTime',
'v': 'verbose',
'verbose': 'verbose',
'd': 'debug',
'debug': 'debug'
};

function processArgs(cmd) {

}

var router = module.exports = function router(app) {
app.use(function (cmd, tty, next) {
cmd.flags._.shift();
cmd.service = new Service({
adapter: cmd.flags.adapter
});

cmd.argv = cmd.flags._;
var file = cmd.argv[0],
options = {},
config,
uid;

cmd.file = file;
cmd.options = options;

if (file) {
//
// Setup pass-thru options for child-process
//
options.options = cmd.argv.splice(cmd.argv.indexOf(file)).splice(1);
}
else if (cmd.flags.c || cmd.flags.command) {
options.options = cmd.argv.splice(cmd.argv.indexOf(cmd.flags.c || cmd.flags.command) + 1);
}
var cli = exports;

//
// Now that we've removed the target script options
// reparse the options and configure the forever settings
//
argv = optimist(cmd.argv).boolean(['v', 'verbose', 'a', 'append', 's', 'silent']).argv;
Object.keys(argv).forEach(function (key) {
if (mappings[key] && argv[key]) {
options[mappings[key]] = argv[key];
}
});
var app = flatiron.app;

if (typeof options['max'] === 'undefined') {
//
// If max isn't specified set it to run forever
//
options.forever = true;
}
app.use(flatiron.plugins.cli, {
usage: forever.cli.usage
});

if (typeof options['minUptime'] !== 'undefined') {
options['minUptime'] = parseFloat(options['minUptime']);
}
if (typeof options['spinSleepTime'] !== 'undefined') {
options['spinSleepTime'] = parseFloat(options['spinSleepTime']);
}
app.config.argv().env();

if (!options.sourceDir) {
//
// Set the sourceDir of the options for graceful
// restarting outside of the main directory
//
options.sourceDir = file && file[0] !== '/' ? process.cwd() : '/';
}

uid = options.uid || utile.randomString(4);
options.uid = uid;
options.pidFile = options.pidFile || uid + '.pid';
options.logFile = argv.l || uid + '.log';

//
// Check for existing global config and set each
// key appropriately if it exists.
//
['append', 'silent', 'verbose'].forEach(function (key) {
var target = mappings[key],
value = forever.config.get(key);

if (value) {
options[target] = options[target] || value === 'true';
}
});
var service = new Service({
adapter: optimist.argv.adapter
});

//
// Pass the source dir to spawn
//
options.spawnWith = {
cwd: options.sourceDir
};

//
// Configure winston for forever based on the CLI options
//
if (options.verbose) {
forever.log.transports.console.level = 'silly';
app.cmd('install', cli.install = function () {
app.service.install(function onInstall(err) {
if (err) {
forevee.log.error(err);
}

//
// Setup configurations for forever
//
config = {
root: cmd.flags.p
};

//
// Only call `forever.load()` if the root path is different than
// the default root exposed by forever.
//
if ((config.root && config.root !== forever.root)) {
forever.log.silly('Loading forever with config: ', config);
forever.load(config);
forever.log.silly('Loaded forever successfully.');
else {
forever.log.info('foreverd installed');
}

next();
});
});

app.cli('/install', function (cmd, tty) {
cmd.service.install(function onInstall(err) {
if (err) {
tty.error(err);
}
else {
tty.info('foreverd installed');
}
});
//TODO
app.cmd('run', cli.run = function () {
service.load(function () {
service.run();
});

//TODO
app.cli('/run', function (cmd, tty) {
cmd.service.load(function () {
cmd.service.run();
});

app.cmd('uninstall', cli.uninstall = function () {
service.uninstall();
});

app.cmd(/add (.*)/, cli.add = function (file) {
service.add(file, forever.cli.getOptions(file));
});

//TODO
app.cmd(/remove (.*)/, cli.remove = function (file) {
service.remove(file, forever.cli.getOptions(file));
});

app.cmd('start', cli.start = function () {
service.start();
});

//TODO
app.cmd('stop', cli.stop = function () {
service.stop();
});

app.cmd('restart', cli.restart = function () {
service.restart();
});

app.cmd('list', cli.list = function () {
service.list(function (err, applications) {
applications.forEach(function printApplication(application) {
console.log(application.monitor.uid, application.monitor.command, application.file, application.monitor.child.pid, application.monitor.logFile, application.monitor.pidFile);
});
});
});

app.cli('/uninstall', function (cmd, tty) {
cmd.service.uninstall();
});

app.cli('/add/*', function (cmd, tty) {
cmd.service.add(cmd.file, cmd.options);
});

//TODO
app.cli('/remove', function (cmd, tty) {
cmd.service.remove(cmd.file, cmd.options);
});

app.cli('/start', function (cmd, tty) {
cmd.service.start();
});
app.cmd('pause', cli.pause = function () {
service.pause();
});

//TODO
app.cli('/stop', function (cmd, tty) {
cmd.service.stop();
});
app.cmd('resume', cli.resume = function () {
service.resume();
});

app.cli('/restart', function (cmd, tty) {
cmd.service.restart();
});

app.cli('/list', function (cmd, tty) {
cmd.service.list(function (err, applications) {
applications.forEach(function printApplication(application) {
console.log(application.monitor.uid, application.monitor.command, application.file, application.monitor.child.pid, application.monitor.logFile, application.monitor.pidFile);
});
});
});

app.cli('/pause', function (cmd, tty) {
cmd.service.pause();
});

app.cli('/resume', function (cmd, tty) {
cmd.service.resume();
});
cli.startCLI = function () {
app.init(app.start);
};

0 comments on commit 079137c

Please sign in to comment.