Last active
July 4, 2018 09:53
-
-
Save octagonal/fb791e58a77e35db034da8fef30fc255 to your computer and use it in GitHub Desktop.
Query an Heroku apps' config vars for a given regex and match it to a given list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
ORG=XXXXX QUERY='(capture_group)' LIST=somelist.txt node queryHeroku.js