Skip to content

Commit

Permalink
Add "env" command for fetching environment variables
Browse files Browse the repository at this point in the history
Outputs dotenv compatible syntax
  • Loading branch information
mraerino committed Dec 30, 2016
1 parent 65b2cc1 commit b4174a7
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
8 changes: 8 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var program = require("commander"),
list = require("../lib/commands/list_sites"),
updateSite = require("../lib/commands/update_site"),
openSite = require("../lib/commands/open"),
env = require("../lib/commands/env"),
updateNotifier = require('update-notifier'),
pkg = require('../package.json');

Expand Down Expand Up @@ -77,6 +78,13 @@ program
.description("Configure continuous deployment")
.action(config.wrap(program, init.cmd));

program
.command("env")
.description("Output configured env variables")
.option("-s --site-id [id]", "Fetch from site with <id>")
.option("-f --file [filename]", "Save to file called <filename>")
.action(config.wrap(program, env.cmd));

program
.command("*","",{noHelp: true})
.action(function(cmd) {
Expand Down
43 changes: 43 additions & 0 deletions lib/commands/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
var chalk = require("chalk"),
inquirer = require("inquirer"),
when = require("when"),
fn = require("when/callbacks"),
fs = require("fs"),
confirm = require("../helpers/confirm"),
site_picker = require("../helpers/site_picker"),
errorLogger = require("../helpers/error_logger");

exports.cmd = function(config, cmd) {
var siteId = config.getSiteId(cmd),
sitePromise = null,
file = cmd.file;

if (siteId) {
sitePromise = config.client.site(siteId);
} else {
sitePromise = site_picker.pickSite(config.client, {});
}

sitePromise
.then(function(site) {
const envVars = site.build_settings.env;
const envContents = Object.keys(envVars).map(function(key) {
return key + '="' + envVars[key] + '"';
}).join('\n');

if(file) {
fs.writeFile(file, envContents, function(err) {
if(err) {
console.error("Could not save file.");
process.exit(1);
} else {
console.log("Saved to " + chalk.bold(file));
process.exit(0);
}
});
} else {
console.log(envContents);
process.exit(0);
}
});
};

0 comments on commit b4174a7

Please sign in to comment.