Skip to content
Browse files

[api] new command 'jitsu tokens list'

  • Loading branch information...
1 parent c0c0dcf commit f9fd9d86e01d1a130ae766e29e1c6244ca01f746 @blakmatrix blakmatrix committed Feb 3, 2013
Showing with 140 additions and 1 deletion.
  1. +2 −1 lib/jitsu.js
  2. +138 −0 lib/jitsu/commands/tokens.js
View
3 lib/jitsu.js
@@ -102,6 +102,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.Tokens = require('nodejitsu-api').Tokens;
//
// ### function welcome ()
@@ -241,7 +242,7 @@ jitsu.setup = function (callback) {
return callback();
}
- ['Users', 'Apps', 'Snapshots', 'Databases', 'Logs'].forEach(function (key) {
+ ['Users', 'Apps', 'Snapshots', 'Databases', 'Logs', 'Tokens'].forEach(function (key) {
var k = key.toLowerCase();
jitsu[k] = new jitsu.api[key](jitsu.config);
jitsu[k].on('debug::request', debug);
View
138 lib/jitsu/commands/tokens.js
@@ -0,0 +1,138 @@
+/*
+ * tokens.js: Commands related to tokens from Nodejitsu.
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var jitsu = require('../../jitsu'),
+ fs = require('fs'),
+ utile = jitsu.common;
+
+var tokens = exports;
+
+tokens.usage = [
+ '`jitsu tokens *` commands enable management of tokens',
+ 'Valid commands are:',
+ '',
+ 'jitsu tokens list',
+ 'jitsu tokens create [<named-token>]',
+ 'jitsu tokens delete <tokenID>',
+];
+
+
+
+tokens.delete = function (tokenID, callback) {
+ //
+ // Allows arbitrary amount of arguments
+ //
+ if(arguments.length) {
+ var args = utile.args(arguments);
+ callback = args.callback;
+ key = args[0] || null;
+ }
+
+ if (key === null) {
+ jitsu.log.error('No token ID specified.');
+ return callback();
+ } else {
+ //delete key
+ }
+};
+
+//
+// Usage for `jitsu tokens delete <key>`
+//
+tokens.delete.usage = [
+ 'Deletes the specified token ID',
+ '',
+ 'jitsu tokens delete <tokenID>'
+];
+
+
+tokens.create = function (tokenName, callback) {
+ //
+ // Allows arbitrary amount of arguments
+ //
+ if(arguments.length) {
+ var args = utile.args(arguments);
+ callback = args.callback;
+ tokenName = args[0] || null;
+ }
+
+ if (key === null) {
+ //creat key
+ } else {
+ //create key with name
+ }
+};
+
+//
+// Usage for `jitsu tokens delete <key>`
+//
+tokens.create.usage = [
+ 'Creates the a token , optionally with a token name',
+ '',
+ 'jitsu tokens create [<token-name>]'
+];
+
+
+tokens.list = function (username, callback) {
+ var authuser = jitsu.config.get('username') || '';
+
+ if(arguments.length) {
+ var args = utile.args(arguments);
+ callback = args.callback;
+ username = args[0] || authuser;
+ }
+
+ if (authuser === '') {
+ return jitsu.commands.users.login(function (err) {
+ if (err) {
+ return callback(err);
+ }
+
+ jitsu.commands.tokens.list(username, callback);
+ });
+ }
+
+ jitsu.log.info('Listing all tokens for ' + username.magenta);
+
+ jitsu.tokens.list(username, function cb(err, result) {
+ if(err){
+ callback(err);
+ } else {
+ var tokens = result.apiTokens;
+ if (!tokens || Object.keys(tokens).length === 0) {
+ jitsu.log.warn('No tokens exist.');
+ jitsu.log.help('Try creating one with ' + 'jitsu tokens create'.magenta);
+ return callback();
+ }
+
+ var rows = [['name', 'token']],
+ colors = ['underline', 'underline'];
+ Object.getOwnPropertyNames(tokens).sort().forEach(function (tokenName) {
+
+ rows.push([
+ tokenName,
+ tokens[tokenName]
+ ]);
+ });
+
+ jitsu.inspect.putRows('data', rows, colors);
+ callback();
+ }
+ });
+
+};
+
+
+//
+// Usage for `jitsu tokens list`
+//
+tokens.list.usage = [
+ 'Lists all tokens in the current user account',
+ '',
+ 'jitsu tokens list'
+];
+

0 comments on commit f9fd9d8

Please sign in to comment.
Something went wrong with that request. Please try again.