Class: Hotdog::Commands::Search
- Inherits:
-
BaseCommand
- Object
- BaseCommand
- Hotdog::Commands::Search
- Defined in:
- lib/hotdog/commands/search.rb
Defined Under Namespace
Classes: AnyHostNode, BinaryExpressionNode, EverythingNode, ExpressionNode, ExpressionParser, ExpressionTransformer, GlobExpressionNode, GlobHostNode, GlobNode, GlobTagNameNode, GlobTagNode, GlobTagValueNode, MultinaryExpressionNode, NothingNode, QueryExpressionNode, RegexpExpressionNode, RegexpHostNode, RegexpNode, RegexpTagNameNode, RegexpTagNode, RegexpTagValueNode, StringExpressionNode, StringHostNode, StringNode, StringTagNameNode, StringTagNode, StringTagValueNode, TagExpressionNode, UnaryExpressionNode
Constant Summary
Constants inherited from BaseCommand
BaseCommand::MASK_DATABASE, BaseCommand::MASK_QUERY, BaseCommand::PERSISTENT_DB
Instance Attribute Summary collapse
-
#remote_command ⇒ Object
readonly
Returns the value of attribute remote_command.
Attributes inherited from BaseCommand
#application, #logger, #options
Instance Method Summary collapse
- #define_options(optparse, options = {}) ⇒ Object
- #evaluate(data, environment) ⇒ Object
- #get_hosts_with_search_tags(result, node) ⇒ Object
- #parse(expression) ⇒ Object
- #parse_options(optparse, args = []) ⇒ Object
- #run(args = [], options = {}) ⇒ Object
Methods inherited from BaseCommand
#execute, #fixed_string?, #initialize, #reload
Constructor Details
This class inherits a constructor from Hotdog::Commands::BaseCommand
Instance Attribute Details
#remote_command ⇒ Object (readonly)
Returns the value of attribute remote_command.
24 25 26 |
# File 'lib/hotdog/commands/search.rb', line 24 def remote_command @remote_command end |
Instance Method Details
#define_options(optparse, options = {}) ⇒ Object
9 10 11 12 13 |
# File 'lib/hotdog/commands/search.rb', line 9 def (optparse, ={}) optparse.on("-n", "--limit LIMIT", "Limit result set to specified size at most", Integer) do |limit| [:limit] = limit end end |
#evaluate(data, environment) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/hotdog/commands/search.rb', line 95 def evaluate(data, environment) node = ExpressionTransformer.new.apply(data) optimized = node.optimize.tap do |optimized| logger.debug { JSON.pretty_generate(optimized.dump) } end optimized.evaluate(environment) end |
#get_hosts_with_search_tags(result, node) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/hotdog/commands/search.rb', line 59 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
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/hotdog/commands/search.rb', line 81 def parse(expression) logger.debug(expression) parser = ExpressionParser.new parser.parse(expression).tap do |parsed| logger.debug { begin JSON.pretty_generate(JSON.load(parsed.to_json)) rescue JSON::NestingError => error error. end } end end |
#parse_options(optparse, args = []) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/hotdog/commands/search.rb', line 15 def (optparse, args=[]) if args.index("--") @remote_command = args.slice(args.index("--") + 1, args.length).join(" ") optparse.parse(args.slice(0, args.index("--"))) else @remote_command = nil optparse.parse(args) end end |
#run(args = [], options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/hotdog/commands/search.rb', line 26 def run(args=[], ={}) if @remote_command logger.warn("ignore remote command: #{@remote_command}") end expression = args.join(" ").strip if expression.empty? # return everything if given expression is empty expression = "*" end begin node = parse(expression) rescue Parslet::ParseFailed => error STDERR.puts("syntax error: " + error.cause.ascii_tree) exit(1) end result0 = evaluate(node, self) if 0 < result0.length result, fields = (result0, node) if [:limit] STDOUT.print(format(result.take([:limit]), fields: fields)) logger.info("found %d host(s), limited to %d in result." % [result.length, [:limit]]) else STDOUT.print(format(result, fields: fields)) logger.info("found %d host(s)." % result.length) end else STDERR.puts("no match found: #{expression}") exit(1) end end |