Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 17b3f58e34
Fetching contributors…

Cannot retrieve contributors at this time

56 lines (42 sloc) 1.455 kb
class CommandBuilder
# parameter names
TermParameters = ['term1', 'term2', 'term3']
FileParameter = 'file'
attr_accessor :params
attr_reader :terms, :filename, :options
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
def valid?
raise InvalidParameterError, "Log file parameter not supplied" unless filename && !filename.empty?
true
end
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
def exec_functions
case File.extname(filename)
when '.gz' then gzip_tools
when '.bz2' then bzip_tools
else default_tools
end
end
def gzip_tools
terms.empty? ? ['gzcat filename'] : ['zgrep options -e term filename'] + ['grep options -e term'] * (terms.size-1)
end
def bzip_tools
terms.empty? ? ['bzcat filename'] : ['bzgrep options -e term filename'] + ['grep options -e term'] * (terms.size-1)
end
def default_tools
terms.empty? ? ['cat filename'] : ['grep options -e term filename'] + ['grep options -e term']* (terms.size-1)
end
class InvalidParameterError < StandardError; end
end
Jump to Line
Something went wrong with that request. Please try again.