Permalink
Browse files

Merge pull request #65 from nicoreed/databases

Databases+users commands have been improved
  • Loading branch information...
2 parents f7aee73 + 35f9d93 commit b21c55d4312d27f821d7b26eea7259b9ad88c61a Nico Reed committed Jul 13, 2011
Showing with 141 additions and 17 deletions.
  1. +1 −1 lib/jitsu/api/databases.js
  2. +5 −0 lib/jitsu/api/users.js
  3. +69 −10 lib/jitsu/commands/databases.js
  4. +66 −6 lib/jitsu/commands/users.js
View
2 lib/jitsu/api/databases.js
@@ -31,7 +31,7 @@ util.inherits(Databases, jitsu.api.Client);
//
Databases.prototype.create = function (databaseType, databaseName, callback) {
this.request('POST', ['databases', this.options.get('username'), databaseName], {type:databaseType}, callback, function (res, result) {
- callback(null, result);
+ callback(null, result, res);
});
};
View
5 lib/jitsu/api/users.js
@@ -81,3 +81,8 @@ Users.prototype.forgot = function (username, callback) {
});
};
+Users.prototype.put = function (username, object, callback) {
+ this.request('PUT', ['users', username], object, callback, function (res, result) {
+ callback(null, result);
+ });
+}
View
79 lib/jitsu/commands/databases.js
@@ -20,20 +20,35 @@ databases.usage = [
];
databases.create = function (databaseType, databaseName, callback) {
+ if (!callback) {
+ winston.error('You need to pass a database name and type');
+ winston.error('jitsu databases create <database type> <database name>');
+ if (!databaseName) {
+ callback = databaseType;
+ }
+ else {
+ callback = databaseName;
+ }
+ return callback();
+ }
+
+
// Make sure that the user is passing a valid database type
if ( ['couch', 'redis', 'mongo'].indexOf(databaseType) === -1 ) {
winston.error('Invalid database type ' + databaseType.red);
winston.info('Expected: `couch`, `redis` or `mongo`');
return callback();
}
- jitsu.databases.create(databaseType, databaseName, function (err) {
- if (err) {
- if (err.statusCode === '409' ) {
+ jitsu.databases.create(databaseType, databaseName, function (err, a, res) {
+ if (err || res.statusCode >= 400) {
+ if ( err && err.statusCode === '409' ) {
winston.error('You already created a database with that name.');
}
else {
- winston.error(err);
+ winston.error('Database could not be created.');
+ winston.error(err || ('Unknown error code: ' + res.statusCode));
+ winston.error('Try again and if it fails again, contact nodejitsu.');
return callback(err);
}
}
@@ -54,6 +69,15 @@ databases.create.usage = [
]
databases.get = function (databaseName, callback) {
+ if (!callback) {
+ winston.error('You need to pass a database name');
+ winston.error('jitsu databases get <database name>');
+ winston.error('If you want to get a list of all the databases run:');
+ winston.error('jitsu databases list');
+ callback = databaseName;
+ return callback();
+ }
+
jitsu.databases.get(databaseName, function (err, results) {
if (err) {
if (err.statusCode === '404') {
@@ -64,9 +88,7 @@ databases.get = function (databaseName, callback) {
}
}
else {
- winston.info('Database name: ' + results.name);
- winston.info('Database type: ' + results.type);
- winston.info('Id: ' + results.metadata.id);
+ printDatabase(results);
}
callback();
});
@@ -87,9 +109,7 @@ databases.list = function (callback) {
else {
results.forEach(function (database) {
winston.info('');
- winston.info('Database name: ' + database.name);
- winston.info('Database type: ' + database.type);
- winston.info('Id: ' + database.metadata.id);
+ printDatabase(database);
});
if (results.length === 0) {
@@ -108,6 +128,13 @@ databases.list.usage = [
]
databases.destroy = function (databaseName, callback) {
+ if (!callback) {
+ winston.error('You need to pass a database name');
+ winston.error('jitsu databases destroy <database name>');
+ callback = databaseName;
+ return callback();
+ }
+
jitsu.databases.destroy(databaseName, function (err) {
if (err) {
winston.error('Database could not be destroyed.');
@@ -126,3 +153,35 @@ databases.destroy.usage = [
'Example usage:',
'jitsu databases destroy <database name>'
]
+
+var printDatabase = function (database) {
+ switch (database.type) {
+ case 'couch':
+ var subdomain = database.metadata.id.split('/')[1];
+ winston.info('Database name: ' + database.name);
+ winston.info('Database type: ' + database.type);
+ winston.info('Connection url: http://' + subdomain + '.couchone.com:5984');
+ break;
+
+ case 'mongo':
+ winston.info('Database name: ' + database.name);
+ winston.info('Database type: ' + database.type);
+ winston.info('Connection url: ' + database.metadata.config.MONGOHQ_URL);
+ break;
+
+ case 'redis':
+ var port = database.metadata.port,
+ password = database.metadata.password,
+ server = database.metadata.label.split('-')[0];
+
+ winston.info('Database name: ' + database.name);
+ winston.info('Database type: ' + database.type);
+ winston.info('Connection url: redis://nodejitsu:' + password+ '@' + server + '.redistogo.com:' + port + '/');
+ break;
+
+ default:
+ winston.info('Database name: ' + database.name);
+ winston.error('Unknown database type: ' + database.type);
+ break;
+ }
+}
View
72 lib/jitsu/commands/users.js
@@ -17,8 +17,9 @@ users.usage = [
'jitsu users create',
'jitsu users available <username>',
'jitsu users confirm <username>',
- 'jitsu users forgot <username>',
+ 'jitsu users forgot <username> [<shake>]',
'jitsu users logout',
+ 'jitsu users changepassword',
'',
'You will be prompted for additional user information',
'as required.'
@@ -66,7 +67,7 @@ users.create = function (callback) {
function getPassword() {
jitsu.prompt.get(['password', {name:'confirm password', hidden: true}], function (err, result) {
- if (result['password'] === result['confirm password'] && result['password'] != "") {
+ if (result['password'] === result['confirm password'] && result['password'] !== "") {
password = result['password'];
var options = {
username: username,
@@ -202,12 +203,18 @@ users.logout.usage = [
// #### @callback {function} Continuation to pass control to when complete.
// Sends the password reset email to the user with the specified `username`.
//
-users.forgot = function (username, callback) {
- if (!callback) {
+users.forgot = function (username, shake, callback) {
+ if (!callback && !shake) {
callback = username;
return callback(new Error('Username is required'), true);
}
-
+
+ if (callback) {
+ // They are providing a shake so lets reset the password
+ return forgotResetHelper(username, shake, callback);
+ }
+
+ callback = shake;
winston.info('Request password reset for: ' + username.magenta);
jitsu.users.forgot(username, function (err, result) {
if (err) {
@@ -219,12 +226,65 @@ users.forgot = function (username, callback) {
});
};
+var forgotResetHelper = function (username, shake, callback) {
+ jitsu.config.set('username', username);
+ jitsu.config.set('password', shake);
+ jitsu.users.auth(function (err) {
+ if (err) {
+ winston.error('Invalid username and shake combo');
+ callback(err);
+ }
+ else {
+ users.changepassword(callback);
+ }
+ });
+}
+
users.forgot.usage = [
'Sends the password reset email to the user with the specified `username`.',
'',
- 'jitsu users forgot <username>'
+ 'jitsu users forgot <username>',
+ '',
+ 'After you have recieved the email, you can run:',
+ 'jitsu users forgot <username> <shake>'
];
+//
+// ### function changepassword (username, callback)
+// #### @callback {function} Continuation to pass control to when complete.
+// Change the password for the user
+//
+users.changepassword = function (callback) {
+ var getPassword = function () {
+ jitsu.prompt.get(['password', {name:'confirm password', hidden: true}], function (err, result) {
+ if (result['password'] === result['confirm password'] && result['password'] !== "") {
+ var options = {password:result['password']};
+ jitsu.users.put(jitsu.config.get('username'), options, function(err) {
+ if (err) {
+ return callback(err);
+ }
+ jitsu.config.save(callback);
+ });
+ }
+ else {
+ if (result['password'] === "") {
+ winston.error('You password must have characters in it');
+ } else {
+ winston.error('The entered passwords do not match');
+ }
+ getPassword();
+ }
+ });
+ };
+ getPassword();
+}
+
+users.changepassword.usage = [
+ 'Used to change the user password',
+ '',
+ 'jitsu users changepassword',
+]
+
users.login = function(callback) {
jitsu.setupUserNoWarn(callback);
}

0 comments on commit b21c55d

Please sign in to comment.