Module: EnvReader
- Defined in:
- lib/envreader.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- RESET =
ANSI escape codes for coloring
"\e[0m"
- GREEN =
"\e[32m"
- RED =
"\e[31m"
- YELLOW =
"\e[33m"
Class Method Summary collapse
-
.cli ⇒ Object
CLI interface for the program.
-
.find_keys(directory = Dir.pwd, extensions: ["rb", "erb"]) ⇒ Object
Find all the used environment keys in the project.
-
.optional_keys(custom_keys = []) ⇒ Object
Define default optional keys.
-
.read_keys(directory = Dir.pwd, extensions: ["rb", "erb"]) ⇒ Object
Find and list all the keys used in the project.
-
.validate_keys(keys, custom_optional_keys = []) ⇒ Object
Validating environment keys.
Class Method Details
.cli ⇒ Object
CLI interface for the program
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/envreader.rb', line 59 def cli = {} OptionParser.new do |opts| opts. = "Usage: env_reader [options]" opts.on("-f", "--find-keys [DIRECTORY]", "Find used keys in a directory (default: current project)") do |dir| [:find_keys] = dir || Dir.pwd end opts.on("-r", "--read-keys [DIRECTORY]", "Read and list all keys used in a directory (default: current project)") do |dir| [:read_keys] = dir || Dir.pwd end opts.on("-v", "--validate-keys", "Validate the environment keys (check if they have values)") do [:validate_keys] = true end opts.on("-e", "--extensions x,y,z", Array, "File extensions to scan (default: rb, erb)") do |ext| [:extensions] = ext end opts.on("-h", "--help", "Prints this help") do puts opts exit end end.parse!(ARGV) if [:read_keys] directory = [:read_keys] validate_directory(directory) extensions = [:extensions] || ["rb", "erb"] puts "#{YELLOW}Reading environment keys from directory: #{directory}#{RESET}" puts "#{YELLOW}File extensions: #{extensions.join(', ')}#{RESET}" read_keys(directory, extensions: extensions) elsif [:find_keys] directory = [:find_keys] validate_directory(directory) extensions = [:extensions] || ["rb", "erb"] puts "#{YELLOW}Scanning directory: #{directory}#{RESET}" puts "#{YELLOW}File extensions: #{extensions.join(', ')}#{RESET}" find_keys(directory, extensions: extensions) elsif [:validate_keys] directory = [:read_keys] || Dir.pwd validate_directory(directory) extensions = [:extensions] || ["rb", "erb"] puts "#{YELLOW}Validating environment keys in directory: #{directory}#{RESET}" all_keys = read_keys(directory, extensions: extensions) validate_keys(all_keys, [:optional_keys] || []) else puts "#{RED}Invalid command. Use --help for usage.#{RESET}" end end |
.find_keys(directory = Dir.pwd, extensions: ["rb", "erb"]) ⇒ Object
Find all the used environment keys in the project
28 29 30 31 32 33 34 35 |
# File 'lib/envreader.rb', line 28 def find_keys(directory = Dir.pwd, extensions: ["rb", "erb"]) usage = scan_files(directory, extensions) .map { |file| [file, extract_keys_from_file(file)] } .to_h .reject { |_, keys| keys.empty? } output_key_usage(usage) end |
.optional_keys(custom_keys = []) ⇒ Object
Define default optional keys
52 53 54 55 56 |
# File 'lib/envreader.rb', line 52 def optional_keys(custom_keys = []) # Add these if you don't want to see them as invalid # %w[CI PIDFILE RAILS_ENV RAILS_MASTER_KEY BUNDLE_GEMFILE HOME LOG_LEVEL DATABASE_URL SECRET_KEY_BASE] + custom_keys %w[BUNDLE_GEMFILE] + custom_keys end |
.read_keys(directory = Dir.pwd, extensions: ["rb", "erb"]) ⇒ Object
Find and list all the keys used in the project
17 18 19 20 21 22 23 24 25 |
# File 'lib/envreader.rb', line 17 def read_keys(directory = Dir.pwd, extensions: ["rb", "erb"]) all_keys = scan_files(directory, extensions) .flat_map { |file| extract_keys_from_file(file) } .uniq puts "#{GREEN}Environment keys found:#{RESET}" all_keys.each { |key| puts "#{GREEN} - #{key}#{RESET}" } all_keys end |
.validate_keys(keys, custom_optional_keys = []) ⇒ Object
Validating environment keys
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/envreader.rb', line 38 def validate_keys(keys, custom_optional_keys = []) invalid_keys = keys.reject do |key| ENV[key].to_s.strip != "" || optional_keys(custom_optional_keys).include?(key) end if invalid_keys.empty? puts "#{GREEN}All environment keys are valid!#{RESET}" else puts "#{RED}Invalid environment keys:#{RESET}" invalid_keys.each { |key| puts "#{RED} - #{key}#{RESET}" } end end |