Skip to content

Commit

Permalink
[api] Continued to stub out core jitsu functionality. Now storing and…
Browse files Browse the repository at this point in the history
… prompting for username / password pair on initial start
  • Loading branch information
indexzero committed Mar 3, 2011
1 parent 641b226 commit de3182f
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 10 deletions.
19 changes: 19 additions & 0 deletions bin/jitsu
@@ -0,0 +1,19 @@
#!/usr/bin/env node

require.paths.unshift(require('path').join(__dirname, '..', 'lib'));

var eyes = require('eyes'),
argv = require('optimist').argv,
jitsu = require('jitsu');

var resources = [
'apps',
'snapshots'
];

var operations = [
'password',

];

jitsu.start(argv._.join(''));
60 changes: 57 additions & 3 deletions lib/jitsu.js
Expand Up @@ -7,11 +7,15 @@

require.paths.unshift(__dirname);

var eyes = require('eyes'),
winston = require('winston'),
colors = require('colors');

var jitsu = exports;

// Failure HTTP Response codes based
// off of `/lib/broodmother/slave/service.js`
var failCodes jitsu.failCodes = {
var failCodes = jitsu.failCodes = {
400: "Bad Request",
404: "Item not found",
500: "Internal Server Error"
Expand All @@ -24,5 +28,55 @@ var successCodes = jitsu.successCodes = {
201: "Created"
};

jitsu.client = require('jitsu/client');
jitsu.apps = require('jitsu/apps');

jitsu.Client = require('jitsu/client').Client;
jitsu.Apps = require('jitsu/apps').Apps;
jitsu.Prompt = require('jitsu/prompt').Prompt;
jitsu.config = require('jitsu/config');

jitsu.start = function (command) {
// Setup the initial prompt but don't leave it open
jitsu.prompt = new 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)
});
}

jitsu.exec(command);
});
};

jitsu.exec = function (command) {
if (!command) {
return winston.error('No command supplied');
}

winston.info('Executing command ' + command.magenta);
};

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();
});
});
};
47 changes: 42 additions & 5 deletions lib/jitsu/apps.js
Expand Up @@ -8,26 +8,63 @@
var util = require('util'),
jitsu = require('jitsu');

//
// ### function Apps (options)
// #### @options {Object} Options for this instance
// Constructor function for the Apps resource responsible
// with Nodejitsu's Apps API
//
var Apps = exports.Apps = function (options) {
jitsu.Client.call(this, options);
};

// Inherit from Client base object
util.inherits(Apps, jitsu.Client);

//
// ### function list (callback)
// #### @callback {function} Continuation to pass control to when complete
// Lists all applications for the authenticated user
//
Apps.prototype.list = function (callback) {

this._request('GET', ['apps'], callback, function (err, res, body) {

});
};

//
// ### function create (app, callback)
// #### @app {Object} Package.json manifest for the application.
// #### @callback {function} Continuation to pass control to when complete
// Creates an application with the specified package.json manifest in `app`.
//
Apps.prototype.create = function (app, callback) {
this._request('POST', ['apps' app.name], app, callback, function (err, res, body) {
this._request('POST', ['apps', app.name], app, callback, function (err, res, body) {

});
};

Apps.prototype.update = function (attrs, callback) {

//
// ### function update (name, attrs, callback)
// #### @name {string} Name of the application to update
// #### @attrs {Object} Attributes to update for this application.
// #### @callback {function} Continuation to pass control to when complete
// Updates the application with `name` with the specified attributes in `attrs`
//
Apps.prototype.update = function (name, attrs, callback) {
this._request('PUT', ['apps', name], attrs, callback, function (err, res, body) {

});
};

//
// ### function destroy (name, callback)
// #### @name {string} Name of the application to destroy
// #### @callback {function} Continuation to pass control to when complete
// Destroys the application with `name` for the authenticated user.
//
Apps.prototype.destory = function (name, callback) {

this._request('DELETE', ['apps', name], callback, function (err, res, body) {

});
};
19 changes: 18 additions & 1 deletion lib/jitsu/client.js
Expand Up @@ -8,10 +8,27 @@
var request = require('request'),
jitsu = require('jitsu');

//
// ### function Client (options)
// #### @options {Object} Options for this instance
// Constructor function for the Client base responsible
// for communicating with Nodejitsu's API
//
var Client = exports.Client = function (options) {

};

//
// ### @private function _request (method, uri, [body], success, callback)
// #### @method {string} HTTP method to use
// #### @uri {string} 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.
// Makes a request to `this.remoteUri + uri` using `method` and any
// `body` (JSON-only) if supplied. Short circuits to `callback` if the response
// code from Nodejitsu matches `jitsu.failCodes`.
//
Client.prototype._request = function (method, uri /* variable arguments */) {
var options, args = Array.prototype.slice.call(arguments),
success = args.pop(),
Expand Down Expand Up @@ -44,7 +61,7 @@ Client.prototype._request = function (method, uri /* variable arguments */) {
}

if (Object.keys(jitsu.failCodes).indexOf(statusCode) !== -1) {
var error = new Error('Broodmother Error (' + statusCode + '): ' + jitsu.failCodes[statusCode]);
var error = new Error('Nodejitsu Error (' + statusCode + '): ' + jitsu.failCodes[statusCode]);
error.result = result;
return callback(error);
}
Expand Down
45 changes: 44 additions & 1 deletion lib/jitsu/config.js
Expand Up @@ -4,4 +4,47 @@
* (C) 2010, Nodejitsu Inc.
*
*/


var path = require('path'),
log = require('jitsu/log');
fs = require('fs');

var config = exports, settings = config.settings = {
root: process.env.HOME,
files: {
config: '.jitsuconf'
}
};

// Export the log configuration
config.log = log.config;

config.load = function (callback) {
fs.readFile(config.file('config'), function (err, data) {
if (err && /ENOENT, No such file/.test(err.message)) {
return config.save(function (err) {
return err ? callback(err) : callback();
});
}
else if (err) {
return callback(err);
}

data = JSON.parse(data.toString());
Object.keys(data).forEach(function (key) {
config.settings[key] = data[key];
});

callback();
});
};

config.save = function (callback) {
fs.writeFile(config.file('config'), JSON.stringify(config.settings), function (err) {
return err ? callback(err) : callback();
});
};

config.file = function (file) {
return path.join(settings.root, settings.files[file]);
};
46 changes: 46 additions & 0 deletions lib/jitsu/log.js
@@ -0,0 +1,46 @@
/*
* log.js: Tools for configuring winston in jitsu.
*
* (C) 2010, Nodejitsu Inc.
*
*/

var winston = require('winston');

var config = exports.config = {
levels: {
silly: 0,
input: 1,
verbose: 2,
prompt: 3,
info: 4,
warn: 5,
debug: 6,
error: 7
},
colors: {
silly: 'magenta',
input: 'grey',
verbose: 'cyan',
prompt: 'grey',
info: 'green',
warn: 'yellow',
debug: 'blue',
error: 'red'
}
};

//
// Configure winston with the levels and colors that we've defined
//
winston.emitErrs = false;
winston.defaultTransports().console.colorize = true;
winston.defaultTransports().console.timestamp = false;
winston.padLevels = true;
winston.setLevels(config.levels);
winston.addColors(config.colors);

//
// TODO (indexzero): Load this in config.js
//
winston.defaultTransports().console.level = 'silly';

0 comments on commit de3182f

Please sign in to comment.