Skip to content

Commit

Permalink
[api docs] Started work on jitsu help commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Mar 11, 2011
1 parent 3401eb7 commit 7445f0b
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 15 deletions.
5 changes: 3 additions & 2 deletions lib/jitsu.js
Expand Up @@ -81,11 +81,12 @@ jitsu.exec = function (command, callback) {
} }


winston.info('Executing command ' + jitsu.commands.parse(command).splice(0,2).join(' ').magenta); winston.info('Executing command ' + jitsu.commands.parse(command).splice(0,2).join(' ').magenta);
jitsu.commands.run(command, function (err) { jitsu.commands.run(command, function (err, shallow) {
if (err && !/403/.test(err.message)) { if (err && !/403/.test(err.message)) {
winston.error('Error running command ' + command.magenta); winston.error('Error running command ' + command.magenta);
winston.error(err.message); winston.error(err.message);
if (err.stack) {
if (err.stack && !shallow) {
err.stack.split('\n').forEach(function (trace) { err.stack.split('\n').forEach(function (trace) {
winston.error(trace); winston.error(trace);
}) })
Expand Down
50 changes: 41 additions & 9 deletions lib/jitsu/commands.js
@@ -1,14 +1,16 @@




var jitsu = require('jitsu'); var winston = require('winston'),
jitsu = require('jitsu');


var commands = exports; var commands = exports;


// Setup the commands organized by resource // Setup the commands organized by resource
commands.commands = { commands.commands = {
apps: require('jitsu/commands/apps'), apps: require('jitsu/commands/apps'),
config: require('jitsu/commands/config'), config: require('jitsu/commands/config'),
snapshots: require('jitsu/commands/snapshots') snapshots: require('jitsu/commands/snapshots'),
help: require('jitsu/commands/help')
}; };


var requireAuth = ['apps', 'users', 'snapshots', 'logs']; var requireAuth = ['apps', 'users', 'snapshots', 'logs'];
Expand Down Expand Up @@ -37,30 +39,60 @@ commands.run = function (command, callback) {
action = parts.shift(), action = parts.shift(),
expected, resource; expected, resource;


//
// If we have been asked for `help` or `usage`
// about a particular resource, print the help
// for that resource.
//
if (action === 'usage' || action === 'help') {
commands.commands.help.show(name);
return callback();
}

if (!commands.commands[name]) { if (!commands.commands[name]) {
return callback(new Error('Cannot run command on unknown resource: ' + name)); return callback(new Error('Cannot run command on unknown resource: ' + name), true);
} }


resource = commands.commands[name]; resource = commands.commands[name];
if (!resource[action]) { if (!resource[action]) {
return callback(new Error('Cannot run unknown action ' + action + ' on resource ' + name)); return callback(new Error('Cannot run unknown action ' + action + ' on resource ' + name), true);
} }


// //
// Append the callback to the arguments to pass the command, // When the command completes, check for an error, and if
// check the arguments length then execute the command. // we are surpressing the err callstack (i.e. it is an **expected** error)
// then print the usage of the specified command.
// //
parts.push(callback); parts.push(function (err, shallow) {
expected = resource[action].length; if (err && shallow) {
winston.help('');
winston.help('Usage:'.bold);
resource[action].usage.forEach(function (line) {
winston.help(line);
});
winston.help('');
}

callback(err, shallow);
});


//
// Check the arguments length then execute the command.
//
expected = resource[action].length;
if (parts.length > expected) { if (parts.length > expected) {
return callback(new Error('Wrong number of arguments: ' + parts.length + ' for ' + expected)) return callback(new Error('Wrong number of arguments: ' + parts.length + ' for ' + expected), true)
} }


function runCommand () { function runCommand () {
resource[action].apply(resource, parts); resource[action].apply(resource, parts);
} }


//
// If this resource can only be accessed
// after authenticating with Nodejitsu, do so
// then run the specified command on the resource
//
if (requireAuth.indexOf(name) !== -1) { if (requireAuth.indexOf(name) !== -1) {
return jitsu.auth(function (err) { return jitsu.auth(function (err) {
if (err) { if (err) {
Expand Down
18 changes: 18 additions & 0 deletions lib/jitsu/commands/apps.js
Expand Up @@ -11,6 +11,24 @@ var eyes = require('eyes'),


var apps = exports; var apps = exports;


apps.usage = [
'`jitsu apps *` commands allow you to work with your',
'Applications on Nodejitsu. Valid commands are:',
'',
'jitsu apps create',
'jitsu apps list',
'jitsu apps view <name>',
'jitsu apps update <name>',
'jitsu apps destroy <name>',
'jitsu apps start <name>',
'jitsu apps restart <name>',
'jitsu apps stop <name>',
'',
'For commands that take a <name> parameter, if no parameter',
'is supplied, jitsu will attempt to read the package.json',
'from the current directory.'
]

// //
// ### function list (callback) // ### function list (callback)
// #### @name {string} **optional** Name of the application to create // #### @name {string} **optional** Name of the application to create
Expand Down
10 changes: 10 additions & 0 deletions lib/jitsu/commands/config.js
Expand Up @@ -11,6 +11,16 @@ var jitsu = require('jitsu'),
var config = exports, var config = exports,
noDelete = ['root', 'remoteUri', 'userconfig', 'auth', 'tmproot', 'tar', 'gzipbin']; noDelete = ['root', 'remoteUri', 'userconfig', 'auth', 'tmproot', 'tar', 'gzipbin'];


config.usage = [
'`jitsu config *` commands allow you to edit your',
'local jitsu configuration file. Valid commands are:',
'',
'jitsu config list',
'jitsu config set <key> <value>',
'jitsu config get <key>',
'jitsu config delete <key>',
];

// //
// ### function set (key, value, callback) // ### function set (key, value, callback)
// #### @key {string} Key to set in jitsu config. // #### @key {string} Key to set in jitsu config.
Expand Down
46 changes: 46 additions & 0 deletions lib/jitsu/commands/help.js
@@ -0,0 +1,46 @@
/*
* help.js: Command related to jitsu help and usage
*
* (C) 2010, Nodejitsu Inc.
*
*/

var winston = require('winston'),
jitsu = require('jitsu');

var help = exports;

help.show = function (name, action) {
var usage, resource = jitsu.commands.commands[name];

if (action && !resource[action]) {
winston.error('No help for command ' + [name, action].join(' ').magenta);
return;
}

usage = action ? resource[action].usage : resource.usage;

if (!usage) {
winston.error('No help for command ' + [name, action].join(' ').magenta);
return;
}

winston.help('');
winston.help('Usage:'.bold);
usage.forEach(function (line) {
winston.help(line);
});
winston.help('');
};

['apps', 'snapshots', 'config'].forEach(function (resource) {
help[resource] = function (action, callback) {
if (!callback) {
callback = action;
action = null;
}

help.show(resource, action);
callback();
};
});
28 changes: 27 additions & 1 deletion lib/jitsu/commands/snapshots.js
Expand Up @@ -10,6 +10,21 @@ var winston = require('winston'),


var snapshots = exports; var snapshots = exports;


snapshots.usage = [
'`jitsu snapshots *` commands allow you to work with snapshots',
'for your Applications on Nodejitsu. Snapshots are images of your',
'Application\'s code that are deployed to the Nodejitsu Platform.',
'Valid commands are: ',
'',
'jitsu snapshots create',
'jitsu snapshots list <name>',
'jitsu snapshots destroy <count>',
'',
'For commands that take a <name> parameter, if no parameter',
'is supplied, jitsu will attempt to read the package.json',
'from the current directory.'
]

// //
// ### function list (name, callback) // ### function list (name, callback)
// #### @name {string} **optional** Name of the application to create // #### @name {string} **optional** Name of the application to create
Expand All @@ -29,7 +44,7 @@ snapshots.list = function (name, callback) {
function executeList () { function executeList () {
jitsu.snapshots.list(name, function (err, snapshots) { jitsu.snapshots.list(name, function (err, snapshots) {
if (err) { if (err) {
return callback(err); return callback(err, true);
} }


if (snapshots && snapshots.length > 0) { if (snapshots && snapshots.length > 0) {
Expand Down Expand Up @@ -57,6 +72,17 @@ snapshots.list = function (name, callback) {
executeList(); executeList();
}; };


//
// Usage for `jitsu snapshots list [<name>]`
//
snapshots.list.usage = [
'Lists all snapshots associated with the application in the',
'current directory or the application with <name>, if supplied',
'',
'jitsu snapshots list',
'jitsu snapshots list <name>'
];

// //
// ### function create (name, callback) // ### function create (name, callback)
// #### @name {string} **optional** Name of the application to create // #### @name {string} **optional** Name of the application to create
Expand Down
8 changes: 5 additions & 3 deletions lib/jitsu/config.js
Expand Up @@ -41,9 +41,10 @@ var log = config.log = {
prompt: 3, prompt: 3,
info: 4, info: 4,
data: 5, data: 5,
warn: 6, help: 6,
debug: 7, warn: 7,
error: 8 debug: 8,
error: 9
}, },
colors: { colors: {
silly: 'magenta', silly: 'magenta',
Expand All @@ -52,6 +53,7 @@ var log = config.log = {
prompt: 'grey', prompt: 'grey',
info: 'green', info: 'green',
data: 'cyan', data: 'cyan',
help: 'yellow',
warn: 'yellow', warn: 'yellow',
debug: 'blue', debug: 'blue',
error: 'red' error: 'red'
Expand Down

0 comments on commit 7445f0b

Please sign in to comment.