Permalink
Browse files

[api doc] Updated core command running implementation. Added more cod…

…e docs.
  • Loading branch information...
1 parent 17ddccb commit fac08e2a20a62a030662986291ab2ab8f7fb0d87 @indexzero indexzero committed Mar 4, 2011
Showing with 244 additions and 63 deletions.
  1. +3 −1 bin/jitsu
  2. +74 −26 lib/jitsu.js
  3. +9 −5 lib/jitsu/client.js
  4. +20 −0 lib/jitsu/config.js
  5. +4 −0 lib/jitsu/log.js
  6. +63 −31 lib/jitsu/prompt.js
  7. +35 −0 lib/jitsu/users.js
  8. +26 −0 lib/jitsu/utils/base64.js
  9. +10 −0 lib/jitsu/utils/index.js
View
@@ -16,4 +16,6 @@ var operations = [
];
-jitsu.start(argv._.join(''));
+jitsu.start(argv._.join(''), function () {
+
+});
View
@@ -11,12 +11,14 @@ var eyes = require('eyes'),
winston = require('winston'),
colors = require('colors');
-var jitsu = exports;
+var jitsu = exports,
+ started = false;
// Failure HTTP Response codes based
// off of `/lib/broodmother/slave/service.js`
var failCodes = jitsu.failCodes = {
400: "Bad Request",
+ 403: "Not Authorized",
404: "Item not found",
500: "Internal Server Error"
};
@@ -28,56 +30,102 @@ var successCodes = jitsu.successCodes = {
201: "Created"
};
-
+jitsu.utils = require('jitsu/utils');
jitsu.Client = require('jitsu/client').Client;
jitsu.Apps = require('jitsu/apps').Apps;
-jitsu.Prompt = require('jitsu/prompt').Prompt;
+jitsu.Users = require('jitsu/users').Users;
+jitsu.prompt = require('jitsu/prompt');
jitsu.config = require('jitsu/config');
-jitsu.start = function (command) {
+//
+// ### function start (command, callback)
+// #### @command {string} Command to execute once started
+// #### @callback {function} Continuation to pass control to when complete.
+// Starts the jitsu CLI and runs the specified command.
+//
+jitsu.start = function (command, callback) {
// Setup the initial prompt but don't leave it open
- jitsu.prompt = new jitsu.Prompt().start().pause();
+ jitsu.prompt.start().pause();
winston.info('Welcome to ' + 'Nodejitsu'.grey);
jitsu.config.load(function (err) {
- if (err) {
- eyes.inspect(err);
- }
-
if (!jitsu.config.settings.auth) {
- return jitsu.setupUser(function () {
- jitsu.exec(command)
+ return jitsu.setupUser(function (err) {
+ return err ? callback(err) : jitsu.exec(command, callback);
});
}
- winston.info('Authenticated as ' + jitsu.config.settings.auth.username.magenta);
- jitsu.exec(command);
+ return err ? callback(err) : jitsu.exec(command, callback);
});
};
-jitsu.exec = function (command) {
+//
+// ### function exec (command, callback)
+// #### @command {string} Command to execute
+// #### @callback {function} Continuation to pass control to when complete.
+// Runs the specified command in the jitsu CLI.
+//
+jitsu.exec = function (command, callback) {
if (!command) {
return winston.error('No command supplied');
}
+ else if (!started) {
+ return jitsu.setup(function (err) {
+ if (err) {
+ return callback(err);
+ }
+
+ runCommand();
+ });
+ }
- winston.info('Executing command ' + command.magenta);
+ function runCommand() {
+ winston.info('Executing command ' + command.magenta);
+ //
+ // d (indexzero): Execute this command
+ //
+ }
+
+ runCommand();
};
+//
+// ### function setup (callback)
+// #### @callback {function} Continuation to pass control to when complete.
+// Sets up the instances of the Resource clients for jitsu and tests
+// the username/password combo for the authenticated user.
+//
+jitsu.setup = function (callback) {
+ jitsu.users = new jitsu.Users(jitsu.config.settings);
+
+ jitsu.users.auth(function (err, success) {
+ started = true;
+
+ if (err || !success) {
+ winston.error('Unable to Autenticate as ' + jitsu.config.settings.auth.username.magenta);
+ winston.error(err.message);
+ return callback(err);
+ }
+
+ winston.info('Authenticated as ' + jitsu.config.settings.auth.username.magenta);
+ callback();
+ });
+};
+
+//
+// ### function setupUser (callback)
+// #### @callback {function} Continuation to pass control to when complete.
+// Prompts the user for their username / password combo, then sets up the jitsu CLI,
+// and saves the resulting configuration to disk.
+//
jitsu.setupUser = function (callback) {
winston.info('No user has been setup on this machine')
jitsu.prompt.get(['username', 'password'], function (result) {
- jitsu.prompt.pause();
-
- //
- // TODO (indexzero): Validate the username and password before saving
- //
jitsu.config.settings.auth = result;
- jitsu.config.save(function (err) {
- if (err) {
- eyes.inspect(err);
- }
-
- return err ? callback(err) : callback();
+ jitsu.setup(function (setupErr) {
+ jitsu.config.save(function (err) {
+ return setupErr || err ? callback(setupErr || err) : callback();
+ });
});
});
};
View
@@ -5,7 +5,8 @@
*
*/
-var request = require('request'),
+var eyes = require('eyes'),
+ request = require('request'),
jitsu = require('jitsu');
//
@@ -15,13 +16,13 @@ var request = require('request'),
// for communicating with Nodejitsu's API
//
var Client = exports.Client = function (options) {
-
+ this.options = options;
};
//
// ### @private function _request (method, uri, [body], success, callback)
// #### @method {string} HTTP method to use
-// #### @uri {string} Locator for the Remote Resource
+// #### @uri {Array} Locator for the Remote Resource
// #### @body {Object} **optional** JSON Request Body
// #### @success {function} Continuation to call upon successful transactions
// #### @callback {function} Continuation to call if errors occur.
@@ -33,12 +34,15 @@ Client.prototype._request = function (method, uri /* variable arguments */) {
var options, args = Array.prototype.slice.call(arguments),
success = args.pop(),
callback = args.pop(),
- body = typeof args[args.length - 1] === 'object' && args.pop();
+ body = typeof args[args.length - 1] === 'object' && args.pop(),
+ userpass = [this.options.auth.username, this.options.auth.password]
+ encoded = jitsu.utils.base64.encode(userpass.join(':'));
options = {
method: method || 'GET',
- uri: this.remoteUri + uri,
+ uri: this.options.remoteUri + '/' + uri.join('/'),
headers: {
+ 'Authorization': 'Basic ' + encoded,
'Content-Type': 'application/json'
}
};
View
@@ -11,6 +11,7 @@ var path = require('path'),
var config = exports, settings = config.settings = {
root: process.env.HOME,
+ remoteUri: 'http://localhost',
files: {
config: '.jitsuconf'
}
@@ -19,6 +20,11 @@ var config = exports, settings = config.settings = {
// Export the log configuration
config.log = log.config;
+//
+// ### function load (callback)
+// #### @callback {function} Continuation to pass control to when complete.
+// Loads the settings for this process.
+//
config.load = function (callback) {
fs.readFile(config.file('config'), function (err, data) {
if (err && /ENOENT, No such file/.test(err.message)) {
@@ -39,12 +45,26 @@ config.load = function (callback) {
});
};
+//
+// ### function save (callback)
+// #### @callback {function} Continuation to pass control to when complete.
+// Saves the settings to the specified location for `.jitsuconf`
+//
config.save = function (callback) {
fs.writeFile(config.file('config'), JSON.stringify(config.settings), function (err) {
return err ? callback(err) : callback();
});
};
+//
+// ### function file (file)
+// #### @file {string} Name of the file to get the path for
+// Gets the full path for the file supplied
+//
config.file = function (file) {
+ if (!settings.files[file]) {
+ return null;
+ }
+
return path.join(settings.root, settings.files[file]);
};
View
@@ -7,6 +7,10 @@
var winston = require('winston');
+//
+// ### @config
+// Log level configuration for jitsu.
+//
var config = exports.config = {
levels: {
silly: 0,
Oops, something went wrong.

0 comments on commit fac08e2

Please sign in to comment.