Class: Hotdog::Commands::Search
- Inherits:
-
BaseCommand
- Object
- BaseCommand
- Hotdog::Commands::Search
- Defined in:
- lib/hotdog/commands/search.rb
Defined Under Namespace
Classes: BinaryExpressionNode, ExpressionNode, ExpressionParser, ExpressionTransformer, TagExpressionNode, TagGlobExpressionNode, TagRegexpExpressionNode, UnaryExpressionNode
Constant Summary
Constants inherited from BaseCommand
Instance Attribute Summary
Attributes inherited from BaseCommand
#application, #logger, #options
Instance Method Summary collapse
- #evaluate(node, environment) ⇒ Object
- #get_hosts_with_search_tags(result, node) ⇒ Object
- #parse(expression) ⇒ Object
- #run(args = []) ⇒ Object
Methods inherited from BaseCommand
#execute, #fixed_string?, #initialize
Constructor Details
This class inherits a constructor from Hotdog::Commands::BaseCommand
Instance Method Details
#evaluate(node, environment) ⇒ Object
73 74 75 76 |
# File 'lib/hotdog/commands/search.rb', line 73 def evaluate(node, environment) node = ExpressionTransformer.new.apply(node) node.evaluate(environment) end |
#get_hosts_with_search_tags(result, node) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/hotdog/commands/search.rb', line 44 def (result, node) drilldown = ->(n){ case when n[:left] && n[:right] then drilldown.(n[:left]) + drilldown.(n[:right]) when n[:expression] then drilldown.(n[:expression]) when n[:identifier] then [n[:identifier]] else [] end } if [:display_search_tags] identifiers = drilldown.call(node).map(&:to_s) if [:primary_tag] = [[:primary_tag]] + identifiers else = identifiers end else = nil end get_hosts(result, ) end |
#parse(expression) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/hotdog/commands/search.rb', line 66 def parse(expression) parser = ExpressionParser.new parser.parse(expression).tap do |parsed| logger.debug(JSON.pretty_generate(JSON.load(parsed.to_json))) end end |
#run(args = []) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/hotdog/commands/search.rb', line 9 def run(args=[]) = { } optparse.on("-n", "--limit LIMIT", "Limit result set to specified size at most", Integer) do |limit| [:limit] = limit end args = optparse.parse(args) expression = args.join(" ").strip if expression.empty? exit(1) end begin node = parse(expression) rescue Parslet::ParseFailed => error STDERR.puts("syntax error: " + error.cause.ascii_tree) exit(1) end result = evaluate(node, self).sort if 0 < result.length _result, fields = (result, node) result = _result.take(.fetch(:limit, _result.size)) STDOUT.print(format(result, fields: fields)) if _result.length == result.length logger.info("found %d host(s)." % result.length) else logger.info("found %d host(s), limited to %d in result." % [_result.length, result.length]) end else STDERR.puts("no match found: #{args.join(" ")}") exit(1) end end |