Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lacostenycoder
Created July 29, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lacostenycoder/f4ec2e4da09201e3ee922143f84adcfd to your computer and use it in GitHub Desktop.
Save lacostenycoder/f4ec2e4da09201e3ee922143f84adcfd to your computer and use it in GitHub Desktop.
Find used and unused environment variables in .env file
#!/usr/bin/env ruby
#Run this in the root of your local github repo to find used env vars located in .env file
vars = File.readlines('.env')
keys = vars.map{|e| e[/^\w+/]}.compact.sort
used_keys = keys.select{|k| !`git grep #{k}`.empty? rescue nil}
puts "THESE ARE USED IN THE PROJECT\n\n"
puts used_keys
puts "\nThese env vars NOT used in codebase\n\n"
puts keys - used_keys
@fragkakis
Copy link

Hello, I propose ignoring the .env file itself from the git grep, or else everything is considered to be used in the project:

#!/usr/bin/env ruby
#Run this in the root of your local github repo to find used env vars located in .env file
vars = File.readlines('.env')
keys = vars.map{|e| e[/^\w+/]}.compact.sort
used_keys = keys.select{|k| puts k;!`git grep #{k} ':!.env'`.empty? rescue nil}
puts "THESE ARE USED IN THE PROJECT\n\n"
puts used_keys
puts "\nThese env vars NOT used in codebase\n\n"
puts keys - used_keys

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment