Class: GrepCommandBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/clarity/commands/grep_command_builder.rb

Direct Known Subclasses

TailCommandBuilder

Defined Under Namespace

Classes: InvalidParameterError

Constant Summary collapse

TermParameters =

parameter names

['term1', 'term2', 'term3']
FileParameter =
'file'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ GrepCommandBuilder

Returns a new instance of GrepCommandBuilder.



10
11
12
13
14
15
16
# File 'lib/clarity/commands/grep_command_builder.rb', line 10

def initialize(params)
  @params   = params
  @filename = params.fetch(FileParameter)
  @terms    = TermParameters.map {|term| params.fetch(term, nil) }.compact.reject {|term| term.empty? }
  @options  = ""
  valid?
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



8
9
10
# File 'lib/clarity/commands/grep_command_builder.rb', line 8

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/clarity/commands/grep_command_builder.rb', line 8

def options
  @options
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/clarity/commands/grep_command_builder.rb', line 7

def params
  @params
end

#termsObject (readonly)

Returns the value of attribute terms.



8
9
10
# File 'lib/clarity/commands/grep_command_builder.rb', line 8

def terms
  @terms
end

Instance Method Details

#bzip_toolsObject



46
47
48
# File 'lib/clarity/commands/grep_command_builder.rb', line 46

def bzip_tools
  terms.empty? ? ['bzcat filename'] : ['bzgrep options -e term filename'] + ['grep options -e term'] * (terms.size-1)
end

#commandObject



23
24
25
26
27
28
29
# File 'lib/clarity/commands/grep_command_builder.rb', line 23

def command
  results = []
  exec_functions.each_with_index do |cmd, index|
    results << cmd.gsub('filename', filename.to_s).gsub('options', options.to_s).gsub('term', terms[index].to_s)
  end
  %[sh -c '#{results.join(" | ")}']
end

#default_toolsObject



50
51
52
# File 'lib/clarity/commands/grep_command_builder.rb', line 50

def default_tools
  terms.empty? ? ['cat filename'] : ['grep options -e term filename'] + ['grep options -e term']* (terms.size-1)
end

#exec_functionsObject



32
33
34
35
36
37
38
# File 'lib/clarity/commands/grep_command_builder.rb', line 32

def exec_functions
  case File.extname(filename)
  when '.gz' then gzip_tools
  when '.bz2' then bzip_tools
  else default_tools
  end
end

#gzip_toolsObject



41
42
43
44
# File 'lib/clarity/commands/grep_command_builder.rb', line 41

def gzip_tools
  cat_tool = (ENV["PATH"].split(":").find{|d| File.exists?(File.join(d, "gzcat"))} ? "zcat" : "gzcat")
  terms.empty? ? ["#{cat_tool} filename"] : ['zgrep options -e term filename'] + ['grep options -e term'] * (terms.size-1)
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:



18
19
20
21
# File 'lib/clarity/commands/grep_command_builder.rb', line 18

def valid?
  raise InvalidParameterError, "Log file parameter not supplied" unless filename && !filename.empty?
  true
end