Skip to content

Instantly share code, notes, and snippets.

@octagonal octagonal/queryHeroku.js
Last active Jul 4, 2018

Embed
What would you like to do?
Query an Heroku apps' config vars for a given regex and match it to a given list
const { execSync } = require('child_process');
const fs = require('fs');
// Convert a newline delimited list to an array, removing all empty values
const lineToArray = lines => lines.split('\n').filter(line => line);
const dbList = lineToArray(fs.readFileSync(process.env.LIST, 'utf8'));
// Get all apps from given org
const herokuApps = lineToArray(execSync(
`heroku apps -o ${process.env.ORG} --json | jq -r ".[] | .name"`,
{ encoding: 'utf8' }
));
// Get all config vars for a list of apps
const herokuKeys = herokuApps.reduce((acc, app) => {
const cmd = `heroku config -s --app ${app}`;
const keys = lineToArray(execSync(
cmd,
{ encoding: 'utf8' }
))
.map(el => el.split('='));
acc.push([app, keys]);
return acc;
}, []);
// Compile
const re = new RegExp(`.*${process.env.QUERY}.*`)
// Filter on a given regular expression and check for the resulting value's presence in a given list
// Expects a regular expression with a single capture group
const found = herokuKeys.filter(
([, vars]) => {
const keyPresent = vars && vars.filter(
([, val]) => {
// Extract the db name and perform a match on it
const name = val.replace(re, '$1');
return dbList.indexOf(name) > -1
}
);
return (keyPresent.length > 0);
}
);
// Return all remaining applications
console.log(found.map(([name]) => name));
@octagonal

This comment has been minimized.

Copy link
Owner Author

commented Jul 4, 2018

Example usage:

ORG=XXXXX QUERY='(capture_group)' LIST=somelist.txt node queryHeroku.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.