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, #logger, #options, #persistent_db_path

Instance Method Summary collapse

Methods inherited from BaseCommand

#define_options, #execute, #fixed_string?, #initialize, #parse_options, #reload

Constructor Details

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

Instance Method Details

#run(args = [], options = {}) ⇒ 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
# File 'lib/hotdog/commands/hosts.rb', line 6

def run(args=[], options={})
  if args.empty?
    result = execute("SELECT id FROM hosts").to_a.reduce(:+)
    show_hosts(result)
  else
    if args.any? { |host_name| glob?(host_name) }
      result = args.flat_map { |host_name|
        execute("SELECT id FROM hosts WHERE LOWER(name) GLOB LOWER(?);", [host_name]).to_a.reduce(:+) || []
      }
    else
      sqlite_limit_compound_select = options[:sqlite_limit_compound_select] || SQLITE_LIMIT_COMPOUND_SELECT
      result = args.each_slice(sqlite_limit_compound_select).flat_map { |args|
        execute("SELECT id FROM hosts WHERE name IN (%s);" % args.map { "?" }.join(", "), args).to_a.reduce(:+) || []
      }
    end
    if result.empty?
      STDERR.puts("no match found: #{args.join(" ")}")
      exit(1)
    else
      show_hosts(result)
      logger.info("found %d host(s)." % result.length)
      if result.length < args.length
        STDERR.puts("insufficient result: #{args.join(" ")}")
        exit(1)
      end
    end
  end
end

#show_hosts(hosts) ⇒ Object



35
36
37
38
# File 'lib/hotdog/commands/hosts.rb', line 35

def show_hosts(hosts)
  result, fields = get_hosts(hosts || [])
  STDOUT.print(format(result, fields: fields))
end