Class: Hotdog::Commands::Hosts

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

Direct Known Subclasses

Ls

Instance Attribute Summary

Attributes inherited from BaseCommand

#application, #confdir, #expiry, #force, #formatter, #listing, #logger, #options, #tags

Instance Method Summary collapse

Methods inherited from BaseCommand

#execute, #fixed_string?, #initialize

Constructor Details

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

Instance Method Details

#run(args = []) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/hotdog/commands/hosts.rb', line 6

def run(args=[])
  if args.empty?
    result = execute("SELECT DISTINCT host_id FROM hosts_tags").to_a.reduce(:+)
  else
    result = args.map { |host_name|
      if glob?(host_name)
        execute("          SELECT DISTINCT hosts_tags.host_id FROM hosts_tags\n            INNER JOIN hosts ON hosts_tags.host_id = hosts.id\n              WHERE LOWER(hosts.name) GLOB LOWER(?);\n        EOS\n      else\n        execute(<<-EOS, host_name).map { |row| row.first }\n          SELECT DISTINCT hosts_tags.host_id FROM hosts_tags\n            INNER JOIN hosts ON hosts_tags.host_id = hosts.id\n              WHERE LOWER(hosts.name) = LOWER(?);\n        EOS\n      end\n    }.reduce(:+)\n  end\n  if 0 < result.length\n    result, fields = get_hosts(result)\n    STDOUT.print(format(result, fields: fields))\n    logger.info(\"found %d host(s).\" % result.length)\n  else\n    STDERR.puts(\"no match found: \#{args.join(\" \")}\")\n    exit(1)\n  end\nend\n", host_name).map { |row| row.first }