Class: UsedEnv

Inherits:
Object
  • Object
show all
Defined in:
lib/used_env.rb

Constant Summary collapse

Reset =
"\e[0m"
Green =
"\e[32m"
Red =
"\e[31m"
Yellow =
"\e[33m"

Class Method Summary collapse

Class Method Details

.categorize_env_variablesObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/used_env.rb', line 30

def self.categorize_env_variables
  categorized = { set: [], unset: [] }

  find_env_variables.each do |env|
    # if ENV[env[:name]]
    value = ENV[env[:name]]
    if value.nil? || value.empty?
      categorized[:unset] << env
    else
      categorized[:set] << env
    end
  end

  categorized
end

.display_envs(check) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/used_env.rb', line 46

def self.display_envs(check)
  categorized = categorize_env_variables
  case check
  when 'valid'
    puts "Set ENV Variables:"
    categorized[:set].each { |env| puts "#{Green}#{env[:name]} -#{Yellow} #{env[:file]}:#{env[:line]} #{Reset} " }
  when 'invalid'
    puts "Unset ENV Variables:"
    categorized[:unset].each { |env| puts "#{Red}#{env[:name]} -#{Yellow} #{env[:file]}:#{env[:line]} #{Reset}" }
  else
    puts "Invalid option! Use 'valid' or 'invalid'."
  end
end

.find_env_variablesObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/used_env.rb', line 13

def self.find_env_variables
  env_vars = []
  
  find_files.each do |file|
    File.foreach(file).with_index do |line, line_num|
      next if line.strip.empty? || line.strip.start_with?('#')

      matches = line.scan(/ENV\[["'](.*?)["']\]|ENV\.fetch\(["'](.*?)["']\)/)
      matches.flatten.compact.each do |env_var|
        env_vars << { name: env_var, file: file, line: line_num + 1 }
      end
    end
  end

  env_vars.uniq
end

.find_filesObject



9
10
11
# File 'lib/used_env.rb', line 9

def self.find_files
  Dir["./**/*.{rb,html,erb,yml}"]
end