Skip to content

Commit

Permalink
add user keys
Browse files Browse the repository at this point in the history
  • Loading branch information
chjj committed Apr 10, 2012
1 parent 25256b2 commit fba2770
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 13 deletions.
27 changes: 14 additions & 13 deletions lib/jitsu.js
Expand Up @@ -34,7 +34,7 @@ jitsu.use(flatiron.plugins.cli, {
string: true
},
jitsuconf: {
alias: 'j',
alias: 'j',
description: 'specify file to load configuration from',
string: true
},
Expand Down Expand Up @@ -69,7 +69,7 @@ jitsu.options.log = {
// Setup config, users, command aliases and prompt settings
//
jitsu.prompt.properties = flatiron.common.mixin(
jitsu.prompt.properties,
jitsu.prompt.properties,
require('./jitsu/properties')
);
jitsu.prompt.override = jitsu.argv;
Expand All @@ -95,6 +95,7 @@ jitsu.api.Databases = require('nodejitsu-api').Databases;
jitsu.api.Logs = require('nodejitsu-api').Logs;
jitsu.api.Snapshots = require('nodejitsu-api').Snapshots;
jitsu.api.Users = require('nodejitsu-api').Users;
// jitsu.api.Keys = require('nodejitsu-api').Keys;

//
// ### function welcome ()
Expand All @@ -111,7 +112,7 @@ jitsu.welcome = function () {
// #### @callback {function} Continuation to pass control to when complete.
// Starts the jitsu CLI and runs the specified command.
//
jitsu.start = function (callback) {
jitsu.start = function (callback) {
//
// Check for --no-colors/--colors option, without hitting the config file
// yet
Expand All @@ -124,7 +125,7 @@ jitsu.start = function (callback) {
if (err) {
return callback();
}

jitsu.init(function (err) {
if (err) {
jitsu.welcome();
Expand Down Expand Up @@ -217,7 +218,7 @@ jitsu.exec = function (command, callback) {
// Sets up the instances of the Resource clients for jitsu.
// there is no io here, yet this function is ASYNC.
//
jitsu.setup = function (callback) {
jitsu.setup = function (callback) {
if (jitsu.started === true) {
return callback();
}
Expand Down Expand Up @@ -258,12 +259,12 @@ jitsu.showError = function (command, err, shallow, skip) {
if (username) {
jitsu.log.error('Unable to authenticate as: ' + username.magenta);
}
jitsu.log.error('403 ' + err.result.error);

jitsu.log.error('403 ' + err.result.error);
}
else if (!skip) {
jitsu.log.error('Error running command ' + command.magenta);

if (!jitsu.config.get('nolog')) {
jitsu.logFile.log(err);
}
Expand All @@ -287,15 +288,15 @@ jitsu.showError = function (command, err, shallow, skip) {
jitsu.log.error('');
jitsu.log.error('This type of error is usually a ' + err.result.result.error.blame.type + ' error.');
}

jitsu.log.error('Error output from your application:');
jitsu.log.error('');
if (err.result.result.error.stdout) {
err.result.result.error.stdout.split('\n').forEach(function (line) {
jitsu.log.error(line);
});
}

if (err.result.result.error.stderr) {
err.result.result.error.stderr.split('\n').forEach(function (line) {
jitsu.log.error(line);
Expand All @@ -306,13 +307,13 @@ jitsu.showError = function (command, err, shallow, skip) {
jitsu.log.error('There was an error while attempting to deploy your application.');
jitsu.log.error('');
jitsu.log.error(err.result.result.error.message);

if (err.result.result.error.blame) {
jitsu.log.error(err.result.result.error.blame.message);
jitsu.log.error('');
jitsu.log.error('This type of error is usually a ' + err.result.result.error.blame.type + ' error.');
}
}

jitsu.log.error('Error output from Haibu:');
jitsu.log.error('');
stack = err.result.result.error.result || err.result.result.error.stack;
Expand Down
153 changes: 153 additions & 0 deletions lib/jitsu/commands/keys.js
@@ -0,0 +1,153 @@
/*
* keys.js: Commands related to user keys
*
* (C) 2010, Nodejitsu Inc.
*
*/

var jitsu = require('../../jitsu');

var keys = exports;

//
// ### function create (username, keyName callback)
// #### @username {string} Username
// #### @data {string} Key data
// #### @callback {function} Continuation to pass control to when complete.
// Attempt to create a key on the user's behalf.
//
keys.create = function (username, data, callback) {
var user = {
username: username,
password: jitsu.config.get('password')
};

if (data.path) {
fs.readFile(data.path, 'utf8', function(err, val) {
if (err) return callback(err);
data.value = val.split(/\s+/)[1];
delete data.path;
return next();
});
} else {
return next();
}

function next() {
jitsu.users.createKey(user, data.name, function (err, response) {
if (err) return callback(err);
jitsu.log.help('Key successfully created.');
});
}
};

//
// Usage for `jitsu keys create`.
//
keys.create.usage = [
'Confirms the Nodejitsu user account for the specified username.',
'You will be prompted to supply a valid invite code for the account.',
'',
'jitsu keys confirm <username> <invitecode>'
];

//
// ### function list (username, callback)
// #### @username {string} Username
// #### @callback {function} Continuation to pass control to when complete.
// Attempt to list all keys bound to the user.
//
keys.list = function (username, callback) {
var user = {
username: username,
password: jitsu.config.get('password')
};

jitsu.users.listKeys(user, function (err, keys) {
if (err) return callback(err);

var rows = [['name', 'state', 'subdomain', 'start', 'latest']],
colors = ['underline', 'underline', 'underline', 'underline', 'underline'];

Object.keys(keys).forEach(function (k) {
var key = keys[k];
rows.push([
k,
key.type,
key.value,
key.ttl,
]);
});

jitsu.inspect.putRows('data', rows, colors);
});
};

//
// Usage for `jitsu keys list`.
//
keys.list.usage = [
'Confirms the Nodejitsu user account for the specified username.',
'You will be prompted to supply a valid invite code for the account.',
'',
'jitsu keys confirm <username> <invitecode>'
];

//
// ### function view (username, keyName callback)
// #### @username {string} Username
// #### @keyName {string} Name of the key
// #### @callback {function} Continuation to pass control to when complete.
// Attempt to return a key.
//
keys.view = function (username, keyName, callback) {
var user = {
username: username,
password: jitsu.config.get('password')
};

jitsu.users.getKey(user, keyName, function (err, key) {
if (err) return callback(err);
jitsu.inspect.putObject(key);
});
};

//
// Usage for `jitsu keys view`.
//
keys.view.usage = [
'Confirms the Nodejitsu user account for the specified username.',
'You will be prompted to supply a valid invite code for the account.',
'',
'jitsu keys confirm <username> <invitecode>'
];

//
// ### function delete (username, keyName callback)
// #### @username {string} Username
// #### @keyName {string} Name of the key
// #### @callback {function} Continuation to pass control to when complete.
// Attempt to return a key.
//
keys.delete = function (username, keyName, callback) {
var user = {
username: username,
password: jitsu.config.get('password')
};

jitsu.users.deleteKey(user, keyName, function (err) {
if (err) return callback(err);
jitsu.log.help('Key deleted successfully.');
});
};

//
// Usage for `jitsu keys delete`.
//
keys.delete.usage = [
'Confirms the Nodejitsu user account for the specified username.',
'You will be prompted to supply a valid invite code for the account.',
'',
'jitsu keys confirm <username> <invitecode>'
];

0 comments on commit fba2770

Please sign in to comment.