Last active
March 29, 2019 16:03
-
-
Save ang3lkar/e09e1a183b2e28fd42535ab02f2900c6 to your computer and use it in GitHub Desktop.
Find the unused ENV variables in your Rails app
This file contains hidden or 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
# All variable names in a nice convenient file, each one in a separate line: | |
# heroku config --app <app> | awk '{print $1}' | sed s/:$//g >> env_variables.txt | |
# or | |
# cat .env | awk -F= '{print $1}' | sed '/^#/ d' | sed s/:$//g | sort >> env_variables.txt | |
variables=`cat env_variables.txt` | |
# new array | |
declare -a zombie_variables | |
for variable in $variables ; | |
do | |
echo "Searching for usage of $variable" | |
# Find occurences of ENV.fetch("foo"), ENV.fetch('foo'), ENV["foo"], ENV['foo'] | |
# (regex stops at $variable, so it also finds ENV.fetch("foo", fallback)) | |
# -m 1 stop reading when string is found | |
# -ERI use regex, recursive search in directories, and ignore binary files | |
# --exclude-dir let's not search the whole universe | |
# --color put some color in your findings | |
if grep -m 1 -ERI --exclude-dir=node_modules --color "ENV(\.fetch\(|\[)[(\"|')+]$variable" . ; then | |
echo "" | |
else | |
echo "$variable NOT FOUND" | |
zombie_variables+=($variable) | |
echo "" | |
fi | |
done | |
echo "ZOMBIE VARIABLES:" | |
# expand and print the array elements in a separate line each | |
printf "%s\n" "${zombie_variables[@]}" | |
printf "%s\n" "${zombie_variables[@]}" >> zombie_variables.txt | |
echo "The end." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment