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, #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

#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
35
36
37
# File 'lib/hotdog/commands/hosts.rb', line 6

def run(args=[])
  update_hosts(@options.dup)

  if args.empty?
    result = execute(<<-EOS).map { |row| row.first }
      SELECT DISTINCT host_id FROM hosts_tags;
    EOS
  else
    result = args.map { |host_name|
      if host_name.index("*")
        execute(<<-EOS, host_name).map { |row| row.first }
          SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
            INNER JOIN hosts ON hosts_tags.host_id = hosts.id
              WHERE LOWER(hosts.name) GLOB LOWER(?);
        EOS
      else
        execute(<<-EOS, host_name).map { |row| row.first }
          SELECT DISTINCT hosts_tags.host_id FROM hosts_tags
            INNER JOIN hosts ON hosts_tags.host_id = hosts.id
              WHERE LOWER(hosts.name) = LOWER(?);
        EOS
      end
    }.reduce(:+)
  end
  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