Created
January 4, 2014 01:56
-
-
Save Sjeanpierre/8250465 to your computer and use it in GitHub Desktop.
List environment variables used by ruby script
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
#!/bin/ruby | |
REGEX = Regexp.new('ENV\[\s*(\S.*?)\s*\]') | |
def report | |
list = list_found | |
if list.empty? | |
puts 'No environment variables found' | |
else | |
list.each {|item| puts "#{item}\n"} | |
end | |
end | |
def list_found | |
file_contents = read_file(ARGV.first) | |
matches = file_contents.scan(REGEX).flatten.uniq | |
matches.reject! {|match| match != match.upcase} | |
matches | |
end | |
def read_file(filename) | |
abort('Error: Filename not specified') unless filename | |
if File.exists?(filename) | |
File.read(filename) | |
else | |
puts "Could not locate file named #{filename}" | |
exit(-1) | |
end | |
end | |
report |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment