Permalink
Browse files

[minor api test] Added `jitsu env *` commands for manipulating applic…

…ation environment variables
  • Loading branch information...
1 parent a770496 commit 15a6176d4261174fb97c94a618de15be5c48d3c5 @indexzero indexzero committed Aug 14, 2011
View
2 lib/jitsu/commands.js
@@ -19,7 +19,7 @@ fs.readdirSync(path.join(__dirname, 'commands')).forEach(function (cmd) {
});
});
-var requireAuth = ['apps', 'databases', 'logs', 'snapshots'];
+var requireAuth = ['apps', 'databases', 'env', 'logs', 'snapshots'];
commands.requiresAuth = function (resource) {
return requireAuth.indexOf(resource) !== -1;
View
3 lib/jitsu/commands/apps.js
@@ -203,7 +203,8 @@ apps.create = function (target, callback) {
});
}
- if (typeof target === 'object') {
+
+ if (target && typeof target === 'object') {
name = target.name;
return executeCreate(null, target);
}
View
2 lib/jitsu/commands/config.js
@@ -59,7 +59,7 @@ config.set.usage = [
config.get = function (key, callback) {
if (!callback) {
callback = key;
- winston.error('No configuration for ' + 'undefined'.magenta);
+ winston.error('No configuration for ' + 'undefined'.yellow);
return callback(new Error(), true, true);
}
View
15 lib/jitsu/commands/databases.js
@@ -23,26 +23,28 @@ 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 ) {
+ 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, a, res) {
if (err || res.statusCode >= 400) {
- if ( err && err.statusCode === '409' ) {
+ if (err && err.statusCode === '409') {
winston.error('You already created a database with that name.');
}
else {
@@ -55,6 +57,7 @@ databases.create = function (databaseType, databaseName, callback) {
else {
winston.info('Database ' + databaseName + ' was created.');
}
+
databases.get(databaseName, callback);
});
};
@@ -162,13 +165,13 @@ var printDatabase = function (database) {
winston.info('Database name: ' + database.name);
winston.info('Database type: ' + database.type);
winston.info('Connection url: http://' + subdomain + '.couchone.com:5984');
- break;
+ 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;
+ break;
case 'redis':
var port = database.metadata.port,
@@ -178,11 +181,11 @@ var printDatabase = function (database) {
winston.info('Database name: ' + database.name);
winston.info('Database type: ' + database.type);
winston.info('Connection url: redis://nodejitsu:' + password+ '@' + server + '.redistogo.com:' + port + '/');
- break;
+ break;
default:
winston.info('Database name: ' + database.name);
winston.error('Unknown database type: ' + database.type);
- break;
+ break;
}
}
View
192 lib/jitsu/commands/env.js
@@ -0,0 +1,192 @@
+/*
+ * env.js: Commands related to environment variables for Nodejitsu applications.
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var winston = require('winston'),
+ jitsu = require('../../jitsu');
+
+var env = exports;
+
+env.usage = [
+ '`jitsu env *` commands allow you to set local environment',
+ 'variables for your application. Valid commands are:',
+ '',
+ 'jitsu env list',
+ 'jitsu env set <key> <value>',
+ 'jitsu env get <key>',
+ 'jitsu env delete <key>',
+ 'jitsu env clear'
+];
+
+//
+// ### function set (key, value, callback)
+// #### @key {string} Key to set in jitsu config.
+// #### @value {string} Value to set the key to.
+// #### @callback {function} Continuation to pass control to when complete
+// Sets the specified `key` in the environment variables to `value` for
+// the application in the current directory.
+//
+env.set = function (key, value, callback) {
+ var args = Array.prototype.slice.call(arguments);
+ callback = args.pop();
+
+ if (args.length !== 2) {
+ winston.error('You must pass both <key> and <value>');
+ return callback(true, true);
+ }
+
+ viewApp(callback, function (err, app) {
+ app.env = app.env || {};
+ app.env[key] = value;
+ jitsu.apps.update(app.name, { env: app.env }, callback);
+ });
+};
+
+//
+// Usage for `jitsu env set <key> <value>`
+//
+env.set.usage = [
+ 'Sets the specified `key` in the environment variables to `value`',
+ 'for the application in the current directory.',
+ '',
+ 'jitsu env set <key> <value>'
+];
+
+//
+// ### function get (key, callback)
+// #### @key {string} Key to get in jitsu config.
+// #### @callback {function} Continuation to pass control to when complete
+// Gets the specified `key` in the environment variables for the
+// application in the current directory.
+//
+env.get = function (key, callback) {
+ if (!callback) {
+ callback = key;
+ winston.error('No environment variables for ' + 'undefined'.yellow);
+ return callback(new Error(), true, true);
+ }
+
+ viewApp(callback, function (err, app) {
+ if (!app.env[key]) {
+ winston.warn('No environment variables for ' + key.yellow);
+ return callback();
+ }
+
+ winston.data([key.yellow, app.env[key].magenta].join(' '));
+ callback();
+ });
+};
+
+//
+// Usage for `jitsu config get <key>`
+//
+env.get.usage = [
+ 'Gets the specified `key` in the environment variables for the',
+ 'application in the current directory.',
+ '',
+ 'jitsu env get <key>'
+];
+
+//
+// ### function delete (key, callback)
+// #### @key {string} Key to delete, in jitsu config.
+// #### @callback {function} Continuation to pass control to when complete
+// Deletes the specified `key` in the environment variables for the
+// application in the current directory.
+//
+env.delete = function (key, callback) {
+ if (!callback) {
+ callback = key;
+ winston.warn('No configuration for ' + 'undefined'.magenta);
+ return callback();
+ }
+
+ var value = jitsu.config.get(key);
+ if (!value) {
+ winston.warn('No configuration value for ' + key.yellow);
+ return callback();
+ }
+
+ viewApp(callback, function (err, app) {
+ if (!app.env[key]) {
+ winston.warn('No environment variables for ' + key.yellow);
+ return callback();
+ }
+
+ delete app.env[key];
+ jitsu.apps.update(app.name, { env: app.env }, callback);
+ });
+};
+
+//
+// Usage for `jitsu env delete <key>`
+//
+env.delete.usage = [
+ 'Deletes the specified `key` in the environment variables for the',
+ 'application in the current directory.',
+ '',
+ 'jitsu env delete <key>'
+];
+
+//
+// ### function list (callback)
+// #### @callback {function} Continuation to pass control to when complete
+// Lists all environment variables for the application in the
+// current directory.
+//
+env.list = function (callback) {
+ viewApp(callback, function (err, app) {
+ if (!app.env || Object.keys(app.env).length === 0) {
+ winston.warn('No environment variables for ' + app.name.magenta);
+ return callback();
+ }
+
+ winston.info('Listing all environment variables for: ' + app.name.magenta);
+ jitsu.log.putObject(app.env);
+ callback();
+ });
+};
+
+//
+// Usage for `jitsu env list`
+//
+env.list.usage = [
+ 'Lists all environment variables for the application in the',
+ 'current directory.',
+ '',
+ 'jitsu env list'
+];
+
+env.clear = function (callback) {
+ viewApp(callback, function (err, app) {
+ if (!app.env || Object.keys(app.env).length === 0) {
+ winston.warn('No environment variables for ' + app.name.magenta);
+ return callback();
+ }
+
+ winston.warn('All environment variables for ' + app.name.magenta + ' will be deleted.');
+ jitsu.prompt.get(['yesno'], function (err, result) {
+ return err
+ ? callback(err)
+ : jitsu.apps.update(app.name, { env: {} }, callback);
+ });
+ });
+};
+
+//
+// ### @private viewApp (callback, success)
+// #### @callback {function} Continuation to respond to on error.
+// #### @success {function} Continuation to respond to on success.
+// Attempts to read the package.json for the current directory
+// and retreive it from Nodejitsu.
+//
+function viewApp (callback, success) {
+ jitsu.package.tryRead(process.cwd(), callback, function (pkg) {
+ jitsu.apps.view(pkg.name, function (err, app) {
+ return err ? callback(err) : success(null, app);
+ });
+ });
+}
View
17 lib/jitsu/commands/help.js
@@ -52,11 +52,12 @@ help.usage = [
'Additional Commands'.cyan.bold.underline,
' jitsu apps',
- ' jitsu snapshots',
- ' jitsu users',
' jitsu logs',
- ' jitsu databases',
+ ' jitsu env',
' jitsu conf',
+ ' jitsu users',
+ ' jitsu databases',
+ ' jitsu snapshots',
' jitsu logout',
'',
'jitsu options'.cyan.bold.underline,
@@ -106,15 +107,9 @@ help.show = function (name, action) {
//
// Setup exports for all relevant resources:
-// `apps`, `snapshots`, `config`.
+// `apps`, `snapshots`, `config`, etc.
//
-
-var allCommands = []
-for (key in jitsu.commands.commands) {
- allCommands.push(key);
-}
-
-allCommands.forEach(function (resource) {
+Object.keys(jitsu.commands.commands).forEach(function (resource) {
help[resource] = function (action, callback) {
if (!callback) {
callback = action;
View
6 lib/jitsu/properties.js
@@ -46,5 +46,11 @@ properties.properties = {
validator: /y[es]?|n[o]?/,
warning: 'Must respond yes or no',
default: 'no'
+ },
+ yesno: {
+ name: 'are you sure?',
+ validator: /y[es]?|n[o]?/,
+ warning: 'Must respond yes or no',
+ default: 'no'
}
};
View
91 test/commands/env-test.js
@@ -0,0 +1,91 @@
+/*
+ * apps.js: Tests for `jitsu apps *` command(s).
+ *
+ * (C) 2010, Nodejitsu Inc.
+ *
+ */
+
+var assert = require('assert'),
+ fs = require('fs'),
+ path = require('path'),
+ mockRequest = require('mock-request'),
+ vows = require('vows'),
+ jitsu = require('../../lib/jitsu'),
+ helper = require('../helpers/mock-helpers');
+
+var mockPrompt2 = helper.mockPrompt2,
+ runJitsuCommand = helper.runJitsuCommand;
+
+vows.describe('jitsu/commands/apps').addBatch({
+ 'This test requires jitsu be unauthorized': function () {
+ jitsu.skipAuth = false;
+ assert.isFalse(jitsu.skipAuth);
+ }
+}).addBatch({
+ 'env list': runJitsuCommand(
+ mockRequest.mock(helper.mockOptions, helper.mockDefaults)
+ .get('/auth')
+ .get('/apps/mickey/jitsu')
+ .respond({
+ body: {
+ app: {
+ name: 'application',
+ state: 'stopped',
+ env: { foo: 'bar', baz: 'buzz' },
+ subdomain:'application',
+ scripts: { start: './server.js' },
+ snapshots: [{ filename: 'FILENAME' }]
+ }
+ }
+ }))
+}).addBatch({
+ 'env get foo': runJitsuCommand(
+ mockRequest.mock(helper.mockOptions, helper.mockDefaults)
+ .get('/apps/mickey/jitsu')
+ .respond({
+ body: {
+ app: {
+ name: 'application',
+ state: 'stopped',
+ env: { foo: 'bar', baz: 'buzz' },
+ subdomain:'application',
+ scripts: { start: './server.js' },
+ snapshots: [{ filename: 'FILENAME' }]
+ }
+ }
+ }))
+}).addBatch({
+ 'env set test truthy': runJitsuCommand(
+ mockRequest.mock(helper.mockOptions, helper.mockDefaults)
+ .get('/apps/mickey/jitsu')
+ .respond({
+ body: {
+ app: {
+ name: 'application',
+ state: 'stopped',
+ env: { foo: 'bar', baz: 'buzz' },
+ subdomain:'application',
+ scripts: { start: './server.js' },
+ snapshots: [{ filename: 'FILENAME' }]
+ }
+ }
+ })
+ .put('/apps/mickey/application'))
+}).addBatch({
+ 'env set delete test': runJitsuCommand(
+ mockRequest.mock(helper.mockOptions, helper.mockDefaults)
+ .get('/apps/mickey/jitsu')
+ .respond({
+ body: {
+ app: {
+ name: 'application',
+ state: 'stopped',
+ env: { foo: 'bar', baz: 'buzz', test: 'truthy' },
+ subdomain:'application',
+ scripts: { start: './server.js' },
+ snapshots: [{ filename: 'FILENAME' }]
+ }
+ }
+ })
+ .put('/apps/mickey/application'))
+}).export(module);

0 comments on commit 15a6176

Please sign in to comment.