Class: Hotdog::Commands::Search
Constant Summary
Constants inherited
from BaseCommand
BaseCommand::MASK_DATABASE, BaseCommand::MASK_QUERY, BaseCommand::PERSISTENT_DB
Instance Attribute Summary collapse
Attributes inherited from BaseCommand
#application, #logger, #options
Instance Method Summary
collapse
Methods inherited from BaseCommand
#execute, #fixed_string?, #initialize, #reload
Instance Attribute Details
#remote_command ⇒ Object
Returns the value of attribute remote_command.
25
26
27
|
# File 'lib/hotdog/commands/search.rb', line 25
def remote_command
@remote_command
end
|
Instance Method Details
#define_options(optparse, options = {}) ⇒ Object
10
11
12
13
14
|
# File 'lib/hotdog/commands/search.rb', line 10
def define_options(optparse, options={})
optparse.on("-n", "--limit LIMIT", "Limit result set to specified size at most", Integer) do |limit|
options[:limit] = limit
end
end
|
#evaluate(data, environment) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/hotdog/commands/search.rb', line 96
def evaluate(data, environment)
node = Hotdog::Expression::ExpressionTransformer.new.apply(data)
if Hotdog::Expression::ExpressionNode === node
optimized = node.optimize.tap do |optimized|
logger.debug {
JSON.pretty_generate(optimized.dump)
}
end
optimized.evaluate(environment)
else
raise("parser error: unknown expression: #{node.inspect}")
end
end
|
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/hotdog/commands/search.rb', line 60
def get_hosts_with_search_tags(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[:tag_name] then [n[:tag_name]]
else []
end
}
if options[:display_search_tags]
tag_names = drilldown.call(node).map(&:to_s)
if options[:primary_tag]
tags = [options[:primary_tag]] + tag_names
else
tags = tag_names
end
else
tags = nil
end
get_hosts(result, tags)
end
|
#parse(expression) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/hotdog/commands/search.rb', line 82
def parse(expression)
logger.debug(expression)
parser = Hotdog::Expression::ExpressionParser.new
parser.parse(expression).tap do |parsed|
logger.debug {
begin
JSON.pretty_generate(JSON.load(parsed.to_json))
rescue JSON::NestingError => error
error.message
end
}
end
end
|
#parse_options(optparse, args = []) ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/hotdog/commands/search.rb', line 16
def parse_options(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
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
58
|
# File 'lib/hotdog/commands/search.rb', line 27
def run(args=[], options={})
if @remote_command
logger.warn("ignore remote command: #{@remote_command}")
end
expression = args.join(" ").strip
if expression.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 = get_hosts_with_search_tags(result0, node)
if options[:limit]
STDOUT.print(format(result.take(options[:limit]), fields: fields))
logger.info("found %d host(s), limited to %d in result." % [result.length, options[: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
|