Skip to content

Commit

Permalink
[feature] added jitsu env load and jitsu env save
Browse files Browse the repository at this point in the history
  • Loading branch information
coderarity committed Nov 29, 2012
1 parent 2f7fc03 commit 3f4c245
Showing 1 changed file with 149 additions and 1 deletion.
150 changes: 149 additions & 1 deletion lib/jitsu/commands/env.js
Expand Up @@ -6,6 +6,7 @@
*/

var jitsu = require('../../jitsu'),
fs = require('fs'),
utile = jitsu.common;

var env = exports;
Expand All @@ -19,7 +20,13 @@ env.usage = [
'jitsu env set <key> <value>',
'jitsu env get <key>',
'jitsu env delete <key>',
'jitsu env clear'
'jitsu env clear',
'jitsu env save',
'jitsu env save <file>',
'jitsu env save <file> <app>',
'jitsu env load',
'jitsu env load <file>',
'jitsu env load <file> <app>'
];

//
Expand Down Expand Up @@ -250,6 +257,147 @@ env.clear = function (appName, callback) {
}
};

//
// ### function save (callback)
// #### @callback {function} Continuation to pass control to when complete
// Outputs all environment variables into env.json for the application in the
// current directory.
//
env.save = function (appName, file, callback) {
//
// Allows arbitrary amount of arguments
//
if(arguments.length) {
var args = utile.args(arguments);
callback = args.callback;
file = args[0] || 'env.json';
appName = args[1] || null;
}

function executeSave(err, app) {
if (!app.env || Object.keys(app.env).length === 0) {
jitsu.log.warn('No environment variables for ' + app.name.magenta);
return callback();
}

jitsu.log.info('Saving all environment variables for ' + app.name.magenta
+ ' to ' + file);
jitsu.log.warn('This may contain sensitive data. Be sure not to commit this'
+ ' file to some public repository. YOU HAVE BEEN WARNED.');
jitsu.log.info('Is this okay?');
jitsu.prompt.confirm('yes/no', function (err, result) {
if (result) {
fs.writeFile(file, JSON.stringify(app.env, null, 2), function (err) {
if (!err) {
jitsu.log.info(file + ' written!');
}
callback(err);
});
}
else {
jitsu.log.info(file + ' not written.');
}
});
}

if (!appName) {
viewApp(callback, executeSave);
}
else {
viewAppByName(appName, callback, executeSave);
}
};

//
// Usage for `jitsu env save`
//
env.save.usage = [
'Save all environment variables to env.json, or to <file> if specified, for the',
'application in the current directory, or the specified <app>.',
'',
'jitsu env save',
'jitsu env save <file>',
'jitsu env save <file> <app>',
];

//
// ### function load (callback)
// #### @callback {function} Continuation to pass control to when complete
// Loads all environment variables from a json file for the application in the
// current directory.
//
env.load = function (appName, file, callback) {
//
// Allows arbitrary amount of arguments
//
if(arguments.length) {
var args = utile.args(arguments);
callback = args.callback;
file = args[0] || 'env.json';
appName = args[1] || null;
}

function executeLoad(err, app) {
var env;
jitsu.log.info('Replacing all environment variables for ' + app.name.magenta
+ ' from variables in ' + file);
fs.readFile(file, function (err, data) {
if (err) {
return callback(err);
}

try {
env = JSON.parse(data);
}
catch (err) {
jitsu.log.error('Error parsing ' + file);
return callback(err);
}

if (env.SUBDOMAIN !== app.env.SUBDOMAIN) {
jitsu.log.error('Refusing to change SUBDOMAIN environment variable.');
return callback(null);
}

jitsu.log.info('Old environment variables: ');
jitsu.inspect.putObject(app.env);
jitsu.log.info('New environment variables: ');
jitsu.inspect.putObject(env);
jitsu.log.warn('YOU CAN LOSE ENVIRONMENT VARIABLES IF YOU ARE NOT CAREFUL.');
jitsu.log.info('Is this okay?');
jitsu.prompt.confirm('yes/no', function (err, result) {
if (result) {
jitsu.apps.update(app.name, { env: env }, callback);
} else {
jitsu.log.info('Environment vaiables for ' + app.name.magenta +
' were not replaced.');
callback(null);
}
});
});
}

if (!appName) {
viewApp(callback, executeLoad);
}
else {
viewAppByName(appName, callback, executeLoad);
}
};

//
// Usage for `jitsu env load`
//
env.load.usage = [
'Replaces all environment variables from env.json, or from <file> if specified,',
'for the application in the current directory, or the specified <app>. Suggested',
'for use with `jitsu env save`. See `jitsu help env save` for more info.',
'',
'jitsu env load',
'jitsu env load <file>',
'jitsu env load <file> <app>',
];

//
// ### @private viewApp (callback, success)
// #### @callback {function} Continuation to respond to on error.
Expand Down

0 comments on commit 3f4c245

Please sign in to comment.