Permalink
Browse files

[refactor] First pass at refactoring jitsu to use `flatiron` and `fla…

…tiron.plugins.cli`
  • Loading branch information...
1 parent 2a50712 commit 8ee37fd5c9ff00d165c915cd7c409aa2c5173f8d @indexzero indexzero committed Feb 7, 2012
View
@@ -1,14 +1,10 @@
#!/usr/bin/env node
-var eyes = require('eyes'),
- colors = require('colors'),
- winston = require('winston').cli(),
- argv = require('optimist').argv,
- jitsu = require('../lib/jitsu');
+var jitsu = require('../lib/jitsu');
-jitsu.start(argv, function (err) {
+jitsu.start(function (err) {
if (!err) {
- winston.info('Nodejitsu'.grey + ' ok'.green.bold);
+ jitsu.log.info('Nodejitsu'.grey + ' ok'.green.bold);
}
process.stdout.on('drain', function () {
View
@@ -5,62 +5,52 @@
*
*/
-var jitsu = exports;
-
-var path = require('path');
-
-// Failure HTTP Response codes based
-// off of `/lib/broodmother/slave/service.js`
-var failCodes = jitsu.failCodes = {
- 400: 'Bad Request',
- 401: 'Unauthorized',
- 403: 'Not Authorized',
- 404: 'Item not found',
- 409: 'Conflict',
- 500: 'Internal Server Error'
-};
+var path = require('path'),
+ colors = require('colors'),
+ flatiron = require('flatiron');
-// Success HTTP Response codes based
-// off of `/lib/broodmother/slave/service.js`
-var successCodes = jitsu.successCodes = {
- 200: 'OK',
- 201: 'Created'
-};
+var jitsu = module.exports = new flatiron.App({
+ //
+ // TODO: What defaults here
+ //
+});
+
+//
+// Configure jitsu to use `flatiron.plugins.cli`
+//
+jitsu.use(flatiron.plugins.cli, {
+ usage: require('./jitsu/usage'),
+ source: path.join(__dirname, 'jitsu', 'commands')
+});
+
+//
+// Setup config, command aliases and prompt settings
+//
+require('./jitsu/config');
+require('./jitsu/alias');
+jitsu.prompt.properties = require('./jitsu/properties').properties;
+jitsu.prompt.override = jitsu.argv;
+
+//
+// Setup other jitsu settings.
+//
+jitsu.skipAuth = false;
+jitsu.started = false;
+jitsu.common = require('./jitsu/common');
+jitsu.package = require('./jitsu/package');
+jitsu.inspect = require('cliff');
+jitsu.logFile = new (require('./jitsu/common/logfile').LogFile)(path.join(process.env.HOME, '.jitsulog'));
-jitsu.skipAuth = false;
-jitsu.started = false;
-jitsu.utils = require('./jitsu/utils');
-jitsu.package = require('./jitsu/package');
-jitsu.log = require('cliff');
-jitsu.logFile = new (require('./jitsu/utils/logfile').LogFile)(path.join(process.env.HOME, '.jitsulog'));
+//
+// Hoist `jitsu.api` from `nodejitsu-api` module.
+//
jitsu.api = {};
jitsu.api.Client = require('nodejitsu-api').Client;
jitsu.api.Apps = require('nodejitsu-api').Apps;
jitsu.api.Databases = require('nodejitsu-api').Databases;
jitsu.api.Logs = require('nodejitsu-api').Logs;
jitsu.api.Snapshots = require('nodejitsu-api').Snapshots;
jitsu.api.Users = require('nodejitsu-api').Users;
-jitsu.prompt = require('prompt');
-jitsu.prompt.properties = require('./jitsu/properties').properties;
-jitsu.prompt.override = require('optimist').argv;
-jitsu.config = require('./jitsu/config');
-jitsu.commands = require('./jitsu/commands');
-
-//
-// Alias the appropriate commands for simplier CLI usage
-//
-jitsu.commands.alias('create', { resource: 'apps', command: 'create' });
-jitsu.commands.alias('deploy', { resource: 'apps', command: 'deploy' });
-jitsu.commands.alias('list', { resource: 'apps', command: 'list' });
-jitsu.commands.alias('conf', { resource: 'config', command: 'list' });
-jitsu.commands.alias('login', { resource: 'users', command: 'login' });
-jitsu.commands.alias('logout', { resource: 'users', command: 'logout' });
-jitsu.commands.alias('signup', { resource: 'users', command: 'create' });
-jitsu.commands.alias('whoami', { resource: 'users', command: 'whoami' });
-
-var eyes = require('eyes'),
- winston = require('winston'),
- colors = require('colors');
//
// Setup `jitsu` to use `pkginfo` to expose version
@@ -73,34 +63,26 @@ require('pkginfo')(module, 'version');
// #### @callback {function} Continuation to pass control to when complete.
// Starts the jitsu CLI and runs the specified command.
//
-jitsu.start = function (argv, callback) {
- var command = argv._;
-
+jitsu.start = function (callback) {
//
- // Special -v command for showing current version without winston formatting
+ // Helper function for dispatching commands
//
- if (argv.version || argv.v) {
- console.log('v' + jitsu.version);
- process.exit(0);
+ function dispatch() {
+ jitsu.router.dispatch('on', jitsu.argv._.join(' '), jitsu.log, callback);
}
-
+
//
// Check for --no-colors/--colors option, without hitting the config file
// yet
//
- (typeof argv.colors == "undefined" || argv.colors) || (colors.mode = "none");
-
- // Setup the initial prompt but don't leave it open
- jitsu.prompt.start().pause();
+ (typeof jitsu.argv.colors == 'undefined' || jitsu.argv.colors) || (colors.mode = "none");
- //
- // Default to the `help` command.
- //
- command[0] || (command[0] = 'help');
-
- jitsu.utils.checkVersion(function (err) {
- if (err) return callback();
- jitsu.config.load(function (err) {
+ jitsu.common.checkVersion(function (err) {
+ if (err) {
+ return callback();
+ }
+
+ jitsu.init(function (err) {
if (err) {
jitsu.welcome();
callback(err);
@@ -113,7 +95,6 @@ jitsu.start = function (argv, callback) {
// https://github.com/nodejitsu/jitsu/issues/101 )
//
jitsu.config.get('colors') || (colors.mode = "none");
-
jitsu.welcome();
var username = jitsu.config.get('username');
@@ -125,12 +106,12 @@ jitsu.start = function (argv, callback) {
}
var username = jitsu.config.get('username');
- winston.info('Successfully configured user ' + username.magenta);
- return command.length > 0 ? jitsu.exec(command, callback) : callback();
+ jitsu.log.info('Successfully configured user ' + username.magenta);
+ return dispatch();
});
}
- return jitsu.exec(command, callback)
+ return dispatch();
});
});
};
@@ -140,8 +121,8 @@ jitsu.start = function (argv, callback) {
// Print welcome message.
//
jitsu.welcome = function () {
- winston.info('Welcome to ' + 'Nodejitsu'.grey);
- winston.info('It worked if it ends with ' + 'Nodejitsu'.grey + ' ok'.green.bold);
+ jitsu.log.info('Welcome to ' + 'Nodejitsu'.grey);
+ jitsu.log.info('It worked if it ends with ' + 'Nodejitsu'.grey + ' ok'.green.bold);
};
//
@@ -160,12 +141,12 @@ jitsu.exec = function (command, callback) {
// Remark: This is a temporary fix for aliasing init=>install,
// was having a few issues with the alias command on the install resource
//
- if (command[0] === 'init'){
+ if (command[0] === 'init') {
command[0] = 'install';
}
// Alias db to databases
- if (command[0] === 'db'){
+ if (command[0] === 'db') {
command[0] = 'databases';
}
@@ -174,7 +155,7 @@ jitsu.exec = function (command, callback) {
command[1] = 'app';
}
- winston.info('Executing command ' + command.join(' ').magenta);
+ jitsu.log.info('Executing command ' + command.join(' ').magenta);
jitsu.commands.run(command, function (err, shallow) {
if (err) {
callback(err);
@@ -223,16 +204,16 @@ jitsu.auth = function (callback) {
}
var username = jitsu.config.get('username');
- winston.silly('Attempting to authenticate as ' + username.magenta);
+ jitsu.log.silly('Attempting to authenticate as ' + username.magenta);
jitsu.users.auth(function (err, success) {
if (err || !success) {
- winston.error('Unable to Authenticate as ' + username.magenta);
- winston.error(err.message);
+ jitsu.log.error('Unable to Authenticate as ' + username.magenta);
+ jitsu.log.error(err.message);
return callback(err);
}
jitsu.skipAuth = true;
- winston.info('Authenticated as ' + username.magenta);
+ jitsu.log.info('Authenticated as ' + username.magenta);
return callback();
})
}
@@ -244,9 +225,9 @@ jitsu.auth = function (callback) {
// and saves the resulting configuration to disk.
//
jitsu.setupUser = function (callback) {
- winston.warn('You will need to login to continue');
- winston.info('To login, you will need an activated nodejitsu account');
- winston.help('You can create an account using the ' + 'jitsu signup'.magenta + ' command');
+ jitsu.log.warn('You will need to login to continue');
+ jitsu.log.info('To login, you will need an activated nodejitsu account');
+ jitsu.log.help('You can create an account using the ' + 'jitsu signup'.magenta + ' command');
jitsu.setupUserNoWarn(callback);
};
@@ -256,7 +237,7 @@ jitsu.setupUser = function (callback) {
// Prompts the user for their username / password combo, then sets up the jitsu CLI,
// and saves the resulting configuration to disk without a warning
//
-jitsu.setupUserNoWarn = function(callback) {
+jitsu.setupUserNoWarn = function (callback) {
//
// Attempt to get the password three times.
//
@@ -292,8 +273,8 @@ jitsu.setupUserNoWarn = function(callback) {
if (err) {
if (tries >= 3) {
- winston.error('Three failed login attempts');
- winston.info('Would you like to reset your password?');
+ jitsu.log.error('Three failed login attempts');
+ jitsu.log.info('Would you like to reset your password?');
return offerReset(result.username);
}
return setupAuth();
@@ -320,76 +301,76 @@ jitsu.showError = function (command, err, shallow, skip) {
var stack;
if (err.statusCode !== '403' && !skip) {
- winston.error('Error running command ' + command.magenta);
+ jitsu.log.error('Error running command ' + command.magenta);
if (!jitsu.config.get('nolog')) {
jitsu.logFile.log(err);
}
if (err.message) {
- winston.error(err.message);
+ jitsu.log.error(err.message);
}
if (err.result) {
if (err.result.error) {
- winston.error(err.result.error);
+ jitsu.log.error(err.result.error);
}
if (err.result.result && err.result.result.error) {
if (err.result.result.error.stderr || err.result.result.error.stdout) {
- winston.error('');
- winston.error('There was an error while attempting to start your application.');
- winston.error(err.result.result.error.message);
+ jitsu.log.error('');
+ jitsu.log.error('There was an error while attempting to start your application.');
+ jitsu.log.error(err.result.result.error.message);
if (err.result.result.error.blame) {
- winston.error(err.result.result.error.blame.message);
- winston.error('');
- winston.error('This type of error is usually a ' + err.result.result.error.blame.type + ' error.');
+ jitsu.log.error(err.result.result.error.blame.message);
+ jitsu.log.error('');
+ jitsu.log.error('This type of error is usually a ' + err.result.result.error.blame.type + ' error.');
}
- winston.error('Error output from your application:');
- winston.error('');
+ jitsu.log.error('Error output from your application:');
+ jitsu.log.error('');
if (err.result.result.error.stdout) {
err.result.result.error.stdout.split('\n').forEach(function (line) {
- winston.error(line);
+ jitsu.log.error(line);
});
}
if (err.result.result.error.stderr) {
err.result.result.error.stderr.split('\n').forEach(function (line) {
- winston.error(line);
+ jitsu.log.error(line);
});
}
}
else if (err.result.result.error.stack && jitsu.config.get('debug')) {
- winston.error('There was an error while attempting to deploy your application.');
- winston.error('');
- winston.error(err.result.result.error.message);
+ jitsu.log.error('There was an error while attempting to deploy your application.');
+ jitsu.log.error('');
+ jitsu.log.error(err.result.result.error.message);
if (err.result.result.error.blame) {
- winston.error(err.result.result.error.blame.message);
- winston.error('');
- winston.error('This type of error is usually a ' + err.result.result.error.blame.type + ' error.');
+ jitsu.log.error(err.result.result.error.blame.message);
+ jitsu.log.error('');
+ jitsu.log.error('This type of error is usually a ' + err.result.result.error.blame.type + ' error.');
}
- winston.error('Error output from Haibu:');
- winston.error('');
+ jitsu.log.error('Error output from Haibu:');
+ jitsu.log.error('');
stack = err.result.result.error.result || err.result.result.error.stack;
stack.split('\n').forEach(function (line) {
- winston.error(line);
+ jitsu.log.error(line);
});
}
}
else if (err.result.stack) {
- winston.warn('Error returned from Nodejitsu');
+ jitsu.log.warn('Error returned from Nodejitsu');
err.result.stack.split('\n').forEach(function (line) {
- winston.error(line);
+ jitsu.log.error(line);
});
}
}
else {
if (err.stack && !shallow) {
err.stack.split('\n').forEach(function (trace) {
- winston.error(trace);
+ jitsu.log.error(trace);
});
}
}
}
- winston.info('Nodejitsu '.grey + 'not ok'.red.bold);
+ jitsu.log.info('Nodejitsu '.grey + 'not ok'.red.bold);
};
View
@@ -0,0 +1,13 @@
+var jitsu = require('../jitsu');
+
+//
+// Alias the appropriate commands for simplier CLI usage
+//
+jitsu.alias('create', { resource: 'apps', command: 'create' });
+jitsu.alias('deploy', { resource: 'apps', command: 'deploy' });
+jitsu.alias('list', { resource: 'apps', command: 'list' });
+jitsu.alias('conf', { resource: 'config', command: 'list' });
+jitsu.alias('login', { resource: 'users', command: 'login' });
+jitsu.alias('logout', { resource: 'users', command: 'logout' });
+jitsu.alias('signup', { resource: 'users', command: 'create' });
+jitsu.alias('whoami', { resource: 'users', command: 'whoami' });
Oops, something went wrong.

0 comments on commit 8ee37fd

Please sign in to comment.