Permalink
Browse files

[api] Added user resource and updated API methods

  • Loading branch information...
1 parent 0ed6b89 commit f63506b5baad4b4888bc1b4130460172a1452bda @indexzero indexzero committed Mar 28, 2011
Showing with 208 additions and 23 deletions.
  1. +29 −17 lib/jitsu.js
  2. +1 −0 lib/jitsu/api/client.js
  3. +36 −0 lib/jitsu/api/users.js
  4. +3 −2 lib/jitsu/commands.js
  5. +3 −2 lib/jitsu/commands/help.js
  6. +132 −0 lib/jitsu/commands/users.js
  7. +4 −2 lib/jitsu/config.js
View
@@ -7,10 +7,6 @@
require.paths.unshift(__dirname);
-var eyes = require('eyes'),
- winston = require('winston'),
- colors = require('colors');
-
var jitsu = exports,
auth = false,
started = false;
@@ -42,6 +38,10 @@ jitsu.prompt = require('jitsu/prompt');
jitsu.config = require('jitsu/config');
jitsu.commands = require('jitsu/commands');
+var eyes = require('eyes'),
+ winston = require('winston'),
+ colors = require('colors');
+
//
// ### function start (command, callback)
// #### @command {string} Command to execute once started
@@ -82,19 +82,8 @@ jitsu.exec = function (command, callback) {
winston.info('Executing command ' + jitsu.commands.parse(command).splice(0, 2).join(' ').magenta);
jitsu.commands.run(command, function (err, shallow) {
- if (err && !/403/.test(err.message)) {
- if (err && err.message) {
- winston.error('Error running command ' + command.magenta);
- winston.error(err.message);
- }
-
- if (err.stack && !shallow) {
- err.stack.split('\n').forEach(function (trace) {
- winston.error(trace);
- });
- }
- winston.info('Nodejitsu '.grey + 'not ok'.red.bold);
- return;
+ if (err) {
+ return jitsu.showError(command, err, shallow);
}
//
@@ -161,4 +150,27 @@ jitsu.setupUser = function (callback) {
});
});
});
+};
+
+jitsu.showError = function (command, err, shallow) {
+ if (err.statusCode !== '403') {
+ winston.error('Error running command ' + command.magenta);
+
+ if (err.message) {
+ winston.error(err.message);
+ }
+
+ if (err.result && err.result.error) {
+ winston.error(err.result.error);
+ }
+ else {
+ if (err.stack && !shallow) {
+ err.stack.split('\n').forEach(function (trace) {
+ winston.error(trace);
+ });
+ }
+ }
+ }
+
+ winston.info('Nodejitsu '.grey + 'not ok'.red.bold);
};
View
@@ -68,6 +68,7 @@ Client.prototype._request = function (method, uri /* variable arguments */) {
if (Object.keys(jitsu.failCodes).indexOf(statusCode) !== -1) {
error = new Error('Nodejitsu Error (' + statusCode + '): ' + jitsu.failCodes[statusCode]);
+ error.statusCode = statusCode;
error.result = result;
return callback(error);
}
View
@@ -32,4 +32,40 @@ Users.prototype.auth = function (callback) {
this._request('GET', ['auth'], callback, function (res, body) {
callback(null, true);
});
+};
+
+//
+// ### function create (user, callback)
+// #### @user {Object} Properties for the new user.
+// #### @callback {function} Continuation to pass control to when complete
+// Creates a new user with the properties specified by `user`.
+//
+Users.prototype.create = function (user, callback) {
+ this._request('POST', ['users', user.username], user, callback, function (res, result) {
+ callback();
+ });
+};
+
+//
+// ### function available (username, callback)
+// #### @username {string} Username to check availability for.
+// #### @callback {function} Continuation to pass control to when complete
+// Checks the availability of the specified `username`.
+//
+Users.prototype.available = function (username, callback) {
+ this._request('GET', ['users', username, 'available'], callback, function (res, result) {
+ callback(null, result);
+ });
+};
+
+//
+// ### function create (user, callback)
+// #### @user {Object} Properties for the user to confirm.
+// #### @callback {function} Continuation to pass control to when complete
+// Confirms the specified `user` by sending the invite code in the `user` specified.
+//
+Users.prototype.confirm = function (user, callback) {
+ this._request('POST', ['users', user.username, 'confirm'], user, callback, function (res, result) {
+ callback(null, result);
+ });
};
View
@@ -9,11 +9,12 @@ var commands = exports;
commands.commands = {
apps: require('jitsu/commands/apps'),
config: require('jitsu/commands/config'),
+ help: require('jitsu/commands/help'),
snapshots: require('jitsu/commands/snapshots'),
- help: require('jitsu/commands/help')
+ users: require('jitsu/commands/users')
};
-var requireAuth = ['apps', 'users', 'snapshots', 'logs'];
+var requireAuth = ['apps', 'snapshots', 'logs'];
//
// ### function parse (command)
@@ -21,7 +21,8 @@ help.usage = [
'Valid resources for `jitsu` are:',
' 1. ' + 'apps'.underline + ': Applications hosted on Nodejitsu',
' 2. ' + 'snapshots'.underline + ': Images of your applications on Nodejitsu',
- ' 3. ' + 'config'.underline + ': Configuration for the jitsu CLI',
+ ' 3. ' + 'users'.underline + ': Nodejitsu user accounts',
+ ' 4. ' + 'config'.underline + ': Configuration for the jitsu CLI',
'',
'For help about a particular resource use: jitsu help <resource>',
'and for help about a particular action use: jitsu help <resource> <action>',
@@ -65,7 +66,7 @@ help.show = function (name, action) {
// Setup exports for all relevant resources:
// `apps`, `snapshots`, `config`.
//
-['apps', 'snapshots', 'config'].forEach(function (resource) {
+['apps', 'snapshots', 'users', 'config'].forEach(function (resource) {
help[resource] = function (action, callback) {
if (!callback) {
callback = action;
View
@@ -0,0 +1,132 @@
+/*
+ * users.js: Commands related to user resources
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var winston = require('winston'),
+ jitsu = require('jitsu');
+
+var users = exports;
+
+users.usage = [
+ '`jitsu users *` commands allow you to work with new',
+ 'or existing Nodejitsu user accounts',
+ '',
+ 'jitsu users create <username>',
+ 'jitsu users available <username>',
+ 'jitsu users confirm <username>',
+ '',
+ 'You will be prompted for additional user information',
+ 'as required.'
+];
+
+//
+// ### function create (username, callback)
+// #### @username {string} Desired username to create
+// #### @callback {function} Continuation to pass control to when complete.
+// Attempts to create a Nodejitsu user account with the specified `username`.
+// Prompts the user for additional `email` and `password` information.
+//
+users.create = function (username, callback) {
+ if (!callback) {
+ callback = username;
+ return callback(new Error('username is required'), true);
+ }
+
+ jitsu.prompt.get(['email', 'password'], function (result) {
+ var user = {
+ email: result.email,
+ password: result.password,
+ username: username
+ };
+
+ winston.info('Creating user ' + username.magenta);
+ jitsu.users.create(user, function (err) {
+ return err ? callback(err) : callback();
+ });
+ });
+};
+
+users.create.usage = [
+ 'Creates a new user account with Nodejitsu. You will be',
+ 'prompted to provide additional `email` and `password` information.',
+ '',
+ 'jitsu users create <username>'
+];
+
+//
+// ### function available (username, callback)
+// #### @username {string} Desired username to check
+// #### @callback {function} Continuation to pass control to when complete.
+// Checks the availability of the specified `username`.
+//
+users.available = function (username, callback) {
+ if (!callback) {
+ callback = username;
+ return callback(new Error('username is required'), true);
+ }
+
+ winston.info('Checking availability for ' + username.magenta);
+ jitsu.users.available(username, function (err, result) {
+ if (err) {
+ return callback(err);
+ }
+
+ var msg = 'Username ' + username.magenta + ' is ';
+ msg += result.available ? 'available'.green : 'not available'.red;
+ winston.info(msg);
+ callback();
+ });
+};
+
+users.available.usage = [
+ 'Checks the availability of the desired username',
+ '',
+ 'jitsu users available <username>'
+];
+
+//
+// ### function confirm (username, callback)
+// #### @username {string} Desired username to confirm
+// #### @callback {function} Continuation to pass control to when complete.
+// Attempts to confirm the Nodejitsu user account with the specified `username`.
+// Prompts the user for additional `inviteCode` information.
+//
+users.confirm = function (username, callback) {
+ if (!callback) {
+ callback = username;
+ return callback(new Error('username is required'), true);
+ }
+
+ jitsu.prompt.get(['Invite code'], function (result) {
+ var user = {
+ username: username,
+ inviteCode: result['invite code']
+ };
+
+ winston.info('Confirming user ' + username.magenta);
+ jitsu.users.confirm(user, function (err, result) {
+ if (err) {
+ return callback(err);
+ }
+
+ if (result.error) {
+ winston.error(result.error);
+ }
+ else {
+ winston.info('User ' + username.magenta + ' confirmed');
+ }
+
+ callback();
+ });
+ });
+};
+
+users.confirm.usage = [
+ 'Confirms the Nodejitsu user account for the specified username.',
+ 'You will be prompted to supply a valid invite code for the account.',
+ '',
+ 'jitsu users confirm <username>'
+];
View
@@ -68,8 +68,10 @@ winston.emitErrs = false;
winston.defaultTransports().console.colorize = true;
winston.defaultTransports().console.timestamp = false;
winston.padLevels = true;
-winston.setLevels(log.levels);
-winston.addColors(log.colors);
+if (!winston.levels.prompt) {
+ winston.setLevels(log.levels);
+ winston.addColors(log.colors);
+}
//
// TODO (indexzero): Load this in config.js

0 comments on commit f63506b

Please sign in to comment.