Class: Glark::Runner

Inherits:
Object
  • Object
show all
Includes:
Logue::Loggable
Defined in:
lib/glark/app/runner.rb

Overview

The main processor.

Constant Summary collapse

GZ_RE =
Regexp.new '\.gz$'
TAR_GZ_RE =
Regexp.new '\.(?:tgz|tar\.gz)$'
TAR_RE =
Regexp.new '\.tar$'
ZIP_RE =
Regexp.new '\.(?:zip|jar)$'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, files) ⇒ Runner

Returns a new instance of Runner.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/glark/app/runner.rb', line 29

def initialize opts, files
  @opts = opts
  @expr = opts.match_spec.expr
  @searched_files = Array.new          # files searched, so we don't cycle through links

  @exclude_matching = @opts.input_spec.exclude_matching

  @range = @opts.input_spec.range
  @output_opts = @opts.output_options
  @invert_match = @output_opts.invert_match

  # 0 == matches, 1 == no matches, 2 == error
  @exit_status = @invert_match ? 0 : 1

  @output_type_cls = @output_opts.output_type_cls
  
  @opts.fileset.each do |type, file|
    search type, file
  end
end

Instance Attribute Details

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



27
28
29
# File 'lib/glark/app/runner.rb', line 27

def exit_status
  @exit_status
end

Instance Method Details

#search(type, name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/glark/app/runner.rb', line 115

def search type, name
  if @exclude_matching
    expr = @opts.match_spec.expr
    return if expr.respond_to?(:re) && expr.re.match(name.to_s)
  end
  
  if name == "-" 
    info "reading standard input..."
    search_text nil, $stdin
  else
    case type
    when :binary
      search_binary name 
    when :text
      search_text name, ::File.new(name)
    when :read
      search_read name 
    when :list
      search_list name 
    else
      raise "type unknown: file: #{name}; type: #{type}"
    end
  end
end

#search_binary(fname) ⇒ Object



66
67
68
69
# File 'lib/glark/app/runner.rb', line 66

def search_binary fname
  file = Glark::BinaryFile.new fname
  update_status file.search_as_binary @expr, @output_opts
end

#search_file(file, output_type_cls = @output_type_cls) ⇒ Object



50
51
52
53
# File 'lib/glark/app/runner.rb', line 50

def search_file file, output_type_cls = @output_type_cls
  output_type = output_type_cls.new file, @output_opts
  update_status file.search @expr, output_type
end

#search_list(fname) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/glark/app/runner.rb', line 95

def search_list fname
  fstr = fname.to_s

  cls = case
        when TAR_RE.match(fstr)
          Glark::TarFile
        when ZIP_RE.match(fstr)
          Glark::ZipFile
        when TAR_GZ_RE.match(fstr)
          Glark::TarGzFile
        else
          write "file '#{fstr}' does not have a handled extension for reading a list"
          return
        end

  file = cls.new fname, @range

  update_status file.search_list(@expr, @output_type_cls, @output_opts)
end

#search_read(fname) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/glark/app/runner.rb', line 77

def search_read fname
  fstr = fname.to_s
  
  case
  when TAR_GZ_RE.match(fstr)
    search_read_archive_file fname, Glark::TarGzFile
  when GZ_RE.match(fstr)
    search_file Glark::GzFile.new(fname, @range)
  when TAR_RE.match(fstr)
    search_read_archive_file fname, Glark::TarFile
  when ZIP_RE.match(fstr)
    search_read_archive_file fname, Glark::ZipFile
  else
    write "file '#{fstr}' does not have a handled extension for reading content"
    return
  end
end

#search_read_archive_file(fname, cls) ⇒ Object



71
72
73
74
75
# File 'lib/glark/app/runner.rb', line 71

def search_read_archive_file fname, cls
  @output_opts.show_file_names = true
  file = cls.new fname, @range
  update_status file.search @expr, @output_type_cls, @output_opts
end

#search_text(fname, io) ⇒ Object



61
62
63
64
# File 'lib/glark/app/runner.rb', line 61

def search_text fname, io
  file = Glark::File.new fname, io, @range
  search_file file
end

#update_status(matched) ⇒ Object



55
56
57
58
59
# File 'lib/glark/app/runner.rb', line 55

def update_status matched
  if matched
    @exit_status = @invert_match ? 1 : 0
  end
end