Class: Hotdog::Commands::Tags

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

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

def run(args=[])
  update_tags(@options.dup)
  if 0 < tags.length
    fields = tags.map { |tag|
      tag_name, tag_value = tag.split(":", 2)
      tag_name
    }
    result1 = fields.map { |tag_name|
      execute("        SELECT DISTINCT tags.value FROM hosts_tags\n          INNER JOIN tags ON hosts_tags.tag_id = tags.id\n            WHERE tags.name = LOWER(?);\n      EOS\n    }\n    result = (0..result1.reduce(0) { |max, values| [max, values.length].max }).map { |field_index|\n      result1.map { |values| values[field_index] }\n    }\n  else\n    fields = [\"tag\"]\n    result = execute(<<-EOS).map { |name, value| [0 < value.length ? \"\#{name}:\#{value}\" : name] }\n      SELECT tags.name, tags.value FROM hosts_tags\n        INNER JOIN tags ON hosts_tags.tag_id = tags.id;\n    EOS\n  end\n  if 0 < result.length\n    STDOUT.puts(format(result, fields: fields))\n  end\nend\n", tag_name).map { |row| row.join(",") }