Browse files

more work on user key commands

  • Loading branch information...
1 parent 33ccc73 commit dcfaf453cbe512c37ba1065222693c6dde7e9ac6 @chjj chjj committed Apr 10, 2012
Showing with 85 additions and 43 deletions.
  1. +1 −1 lib/jitsu.js
  2. +84 −42 lib/jitsu/commands/keys.js
View
2 lib/jitsu.js
@@ -95,7 +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;
+jitsu.api.Keys = require('nodejitsu-api').Keys;
//
// ### function welcome ()
View
126 lib/jitsu/commands/keys.js
@@ -5,77 +5,106 @@
*
*/
-var jitsu = require('../../jitsu');
+var jitsu = require('../../jitsu'),
+ fs = require('fs');
var keys = exports;
//
-// ### function create (username, keyName callback)
+// ### function create (name, path, 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')
- };
+keys.create = function (keyName, file, callback) {
+ var user = jitsu.config.get('username');
+ var data = { name: keyName };
- if (data.path) {
- fs.readFile(data.path, 'utf8', function(err, val) {
+ function read(file) {
+ fs.readFile(file, 'utf8', function(err, val) {
if (err) return callback(err);
- val = val.split(/\s+/);
- data.value = val[1] || val[0];
- delete data.path;
- return next();
+
+ if (/private key/i.test(val)) {
+ jitsu.log.error(
+ 'Private key detected. Stopping.'
+ + ' Are you sure this is a public key?');
+ return callback(new Error(), true);
+ }
+
+ data.value = val;
+
+ return create(data);
});
- } else {
- return next();
}
- function next() {
- jitsu.keys.create(user, data, function (err, response) {
+ function create(data) {
+ jitsu.keys.create(user + '/' + keyName, data, function (err, response) {
if (err) return callback(err);
jitsu.log.help('Key successfully created.');
});
}
+
+ if (!keyName) {
+ jitsu.log.info('Please enter a name for your key.');
+ return jitsu.prompt.get(['name'], function(err, result) {
+ return err
+ ? callback(err)
+ : keys.create(result.name, file, callback);
+ });
+ }
+
+ if (file) return read(file);
+
+ jitsu.log.info('Type of key? (api/ssh)');
+ jitsu.prompt.get(['type'], function (err, result) {
+ if (result.type === 'ssh') {
+ jitsu.log.info('Please enter location of public key.');
+ jitsu.log.info('~/.ssh/id_rsa.pub will be used by default.');
+ jitsu.prompt.get(['file'], function (err, result) {
+ file = result.file || jitsu.config.get('root') + '/.ssh/id_rsa.pub';
+ return read(file);
+ });
+ return;
+ }
+ jitsu.log.info('Generating API key...');
+ return create(data);
+ });
};
//
// Usage for `jitsu keys create`.
//
keys.create.usage = [
- 'Adds a key to jitsu, if no path is specified, generate random API key.',
+ 'Adds a key to jitsu, if no file is specified, generate random API key.',
'',
- 'jitsu keys create <name> <path>'
+ 'jitsu keys create',
+ 'jitsu keys create <name>',
+ 'jitsu keys create <name> <file>'
];
//
-// ### function list (username, callback)
+// ### function list (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')
- };
+keys.list = function (callback) {
+ var user = jitsu.config.get('username');
jitsu.keys.list(user, function (err, keys) {
if (err) return callback(err);
- var rows = [['name', 'state', 'subdomain', 'start', 'latest']],
- colors = ['underline', 'underline', 'underline', 'underline', 'underline'];
+ var rows = [['name', 'type', 'value', 'ttl']],
+ colors = ['underline', 'underline', 'underline', 'underline'];
Object.keys(keys).forEach(function (k) {
var key = keys[k];
rows.push([
k,
key.type,
- key.value,
- key.ttl,
+ key.value.substring(0, 40),
+ key.ttl || 'none'
]);
});
@@ -99,13 +128,19 @@ keys.list.usage = [
// #### @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')
- };
+keys.view = function (keyName, callback) {
+ var user = jitsu.config.get('username');
- jitsu.keys.view(user, keyName, function (err, key) {
+ if (!keyName) {
+ jitsu.log.info('Please enter a name for your key.');
+ return jitsu.prompt.get(['name'], function(err, result) {
+ return err
+ ? callback(err)
+ : keys.view(result.name, callback);
+ });
+ }
+
+ jitsu.keys.view(user + '/' + keyName, function (err, key) {
if (err) return callback(err);
jitsu.inspect.putObject(key);
});
@@ -117,6 +152,7 @@ keys.view = function (username, keyName, callback) {
keys.view.usage = [
'View a specific API or SSH key by name.',
'',
+ 'jitsu keys view',
'jitsu keys view <name>'
];
@@ -127,14 +163,20 @@ keys.view.usage = [
// #### @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')
- };
+keys.delete = function (keyName, callback) {
+ var user = jitsu.config.get('username');
+
+ if (!keyName) {
+ jitsu.log.info('Please enter a name for your key.');
+ return jitsu.prompt.get(['name'], function(err, result) {
+ return err
+ ? callback(err)
+ : keys.delete(result.name, callback);
+ });
+ }
// fix this ins nodejitsu-api
- jitsu.keys.destroy(user, keyName, function (err) {
+ jitsu.keys.destroy(user + '/' + keyName, function (err) {
if (err) return callback(err);
jitsu.log.help('Key deleted successfully.');
});
@@ -146,6 +188,6 @@ keys.delete = function (username, keyName, callback) {
keys.delete.usage = [
'Delete the specified API or SSH key.',
'',
+ 'jitsu keys delete',
'jitsu keys delete <name>'
];
-

0 comments on commit dcfaf45

Please sign in to comment.