Class: HTTPDisk::Grep::Main
- Inherits:
-
Object
- Object
- HTTPDisk::Grep::Main
- Defined in:
- lib/httpdisk/grep/main.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
-
#initialize(options) ⇒ Main
constructor
A new instance of Main.
-
#paths ⇒ Object
file paths to be searched.
-
#pattern ⇒ Object
regex pattern from options.
-
#prepare_body(payload) ⇒ Object
convert raw body into something palatable for pattern matching.
-
#printer ⇒ Object
printer for output.
-
#run ⇒ Object
Enumerate file paths one at a time.
- #run_one(path) ⇒ Object
Constructor Details
#initialize(options) ⇒ Main
Returns a new instance of Main.
9 10 11 |
# File 'lib/httpdisk/grep/main.rb', line 9 def initialize() @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/httpdisk/grep/main.rb', line 7 def @options end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
7 8 9 |
# File 'lib/httpdisk/grep/main.rb', line 7 def success @success end |
Instance Method Details
#paths ⇒ Object
file paths to be searched
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/httpdisk/grep/main.rb', line 51 def paths # roots roots = [:roots] roots = ['.'] if roots.empty? # find files in roots paths = roots.flat_map { Find.find(_1).to_a }.sort paths = paths.select { File.file?(_1) } # strip default './' paths = paths.map { _1.gsub(%r{^\./}, '') } if [:roots].empty? paths end |
#pattern ⇒ Object
regex pattern from options
93 94 95 |
# File 'lib/httpdisk/grep/main.rb', line 93 def pattern @pattern ||= Regexp.new([:pattern], Regexp::IGNORECASE) end |
#prepare_body(payload) ⇒ Object
convert raw body into something palatable for pattern matching
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 |
# File 'lib/httpdisk/grep/main.rb', line 66 def prepare_body(payload) body = payload.body if content_type = payload.headers['Content-Type'] # Mismatches between Content-Type and body.encoding are fatal, so make # an effort to align them. if charset = content_type[/charset=([^;]+)/, 1] encoding = begin Encoding.find(charset) rescue StandardError nil end if encoding && body.encoding != encoding body.force_encoding(encoding) end end # pretty print json for easier searching if content_type =~ /\bjson\b/ body = JSON.pretty_generate(JSON.parse(body)) end end body end |
#printer ⇒ Object
printer for output
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/httpdisk/grep/main.rb', line 98 def printer @printer ||= case when [:silent] Grep::SilentPrinter.new when [:count] Grep::CountPrinter.new($stdout) when [:head] || $stdout.tty? Grep::HeaderPrinter.new($stdout, [:head]) else Grep::TersePrinter.new($stdout) end end |
#run ⇒ Object
Enumerate file paths one at a time. Returns true if matches were found.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/httpdisk/grep/main.rb', line 14 def run paths.each do begin run_one(_1) rescue StandardError => e if ENV['HTTPDISK_DEBUG'] $stderr.puts $stderr.puts e.class $stderr.puts e.backtrace.join("\n") end raise CliError, "#{e.[0, 70]} (#{_1})" end end success end |
#run_one(path) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/httpdisk/grep/main.rb', line 30 def run_one(path) # read payload & body payload = Zlib::GzipReader.open(path, encoding: 'ASCII-8BIT') do Payload.read(_1) end body = prepare_body(payload) # collect all_matches all_matches = body.each_line.map do |line| [].tap do |matches| line.scan(pattern) { matches << Regexp.last_match } end end.reject(&:empty?) return if all_matches.empty? # print @success = true printer.print(path, payload, all_matches) end |