Class: Hotdog::Commands::Search

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

Direct Known Subclasses

SshAlike

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

Attributes inherited from BaseCommand

#application, #logger, #options

Instance Method Summary collapse

Methods inherited from BaseCommand

#execute, #fixed_string?, #initialize, #reload

Constructor Details

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

Instance Attribute Details

#remote_commandObject (readonly)

Returns the value of attribute remote_command.



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

def remote_command
  @remote_command
end

Instance Method Details

#define_options(optparse, options = {}) ⇒ Object



22
23
24
25
26
# File 'lib/hotdog/commands/search.rb', line 22

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



108
109
110
111
112
113
114
115
116
# File 'lib/hotdog/commands/search.rb', line 108

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/hotdog/commands/search.rb', line 72

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[:identifier] then [n[:identifier]]
    else []
    end
  }
  if options[:display_search_tags]
    identifiers = drilldown.call(node).map(&:to_s)
    if options[:primary_tag]
      tags = [options[:primary_tag]] + identifiers
    else
      tags = identifiers
    end
  else
    tags = nil
  end
  get_hosts(result, tags)
end

#parse(expression) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hotdog/commands/search.rb', line 94

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.message
      end
    }
  end
end

#parse_options(optparse, args = []) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/hotdog/commands/search.rb', line 28

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hotdog/commands/search.rb', line 39

def run(args=[], options={})
  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 = 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