Class: Hotdog::Commands::Search

Inherits:
BaseCommand show all
Defined in:
lib/hotdog/commands/search.rb

Defined Under Namespace

Classes: BinaryExpressionNode, ExpressionNode, ExpressionParser, ExpressionTransformer, TagExpressionNode, TagGlobExpressionNode, TagRegexpExpressionNode, UnaryExpressionNode

Instance Attribute Summary

Attributes inherited from BaseCommand

#application, #formatter, #logger, #options, #tags

Instance Method Summary collapse

Methods inherited from BaseCommand

#execute, #initialize

Constructor Details

This class inherits a constructor from Hotdog::Commands::BaseCommand

Instance Method Details

#evaluate(node, environment) ⇒ Object



41
42
43
44
# File 'lib/hotdog/commands/search.rb', line 41

def evaluate(node, environment)
  node = ExpressionTransformer.new.apply(node)
  node.evaluate(environment)
end

#parse(expression) ⇒ Object



34
35
36
37
38
39
# File 'lib/hotdog/commands/search.rb', line 34

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=[])
  expression = args.join(" ").strip
  if expression.empty?
    exit(1)
  end

  update_hosts(@options.dup)
#       update_tags(@options.dup)

  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 = get_hosts(result)
    STDOUT.puts(format(result, fields: fields))
  else
    STDERR.puts("no match found: #{args.join(" ")}")
    exit(1)
  end
end