Class: HTTPDisk::Grep::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/httpdisk/grep/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/httpdisk/grep/main.rb', line 7

def options
  @options
end

#successObject (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

#pathsObject

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 = options[: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 options[:roots].empty?
  paths
end

#patternObject

regex pattern from options



93
94
95
# File 'lib/httpdisk/grep/main.rb', line 93

def pattern
  @pattern ||= Regexp.new(options[: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

#printerObject

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 options[:silent]
    Grep::SilentPrinter.new
  when options[:count]
    Grep::CountPrinter.new($stdout)
  when options[:head] || $stdout.tty?
    Grep::HeaderPrinter.new($stdout, options[:head])
  else
    Grep::TersePrinter.new($stdout)
  end
end

#runObject

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.message[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