Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[feature] added `jitsu env load` and `jitsu env save`

  • Loading branch information...
commit 0e712aae7c09f13613312d66424fa464257cc8c9 1 parent b04a5cc
@coderarity coderarity authored blakmatrix committed
Showing with 149 additions and 1 deletion.
  1. +149 −1 lib/jitsu/commands/env.js
View
150 lib/jitsu/commands/env.js
@@ -6,6 +6,7 @@
*/
var jitsu = require('../../jitsu'),
+ fs = require('fs'),
utile = jitsu.common;
var env = exports;
@@ -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>'
];
//
@@ -251,6 +258,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.
// #### @success {function} Continuation to respond to on success.
Please sign in to comment.
Something went wrong with that request. Please try again.