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
55 56 57 58 |
# File 'lib/hotdog/commands/search.rb', line 55 def evaluate(node, environment) node = ExpressionTransformer.new.apply(node) node.evaluate(environment) end |
#get_hosts_with_search_tags(result, node) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hotdog/commands/search.rb', line 34 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 } identifiers = drilldown.call(node).map(&:to_s) get_hosts(result, [:display_search_tags] ? identifiers : []) end |
#parse(expression) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/hotdog/commands/search.rb', line 48 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 |
# File 'lib/hotdog/commands/search.rb', line 9 def run(args=[]) 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) STDOUT.print(format(result, fields: fields)) logger.info("found %d host(s)." % result.length) else STDERR.puts("no match found: #{args.join(" ")}") exit(1) end end |