Skip to content

Instantly share code, notes, and snippets.

@Sjeanpierre
Created January 4, 2014 01:56
Show Gist options
  • Save Sjeanpierre/8250465 to your computer and use it in GitHub Desktop.
Save Sjeanpierre/8250465 to your computer and use it in GitHub Desktop.
List environment variables used by ruby script
#!/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