Class: Hotdog::Commands::Tags

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

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
35
36
37
38
39
40
41
# File 'lib/hotdog/commands/tags.rb', line 6

def run(args=[])
  if 0 < tags.length
    fields = tags.map { |tag|
      tag_name, tag_value = tag.split(":", 2)
      tag_name
    }
    result1 = fields.map { |tag_name|
      if not glob?(tag_name)
        execute(<<-EOS, tag_name).map { |row| row.join(",") }
          SELECT DISTINCT tags.value FROM hosts_tags
            INNER JOIN tags ON hosts_tags.tag_id = tags.id
              WHERE tags.name = LOWER(?);
        EOS
      else
        execute(<<-EOS, tag_name).map { |row| row.join(",") }
          SELECT DISTINCT tags.value FROM hosts_tags
            INNER JOIN tags ON hosts_tags.tag_id = tags.id
              WHERE tags.name GLOB LOWER(?);
        EOS
      end
    }
    result = (0..result1.reduce(0) { |max, values| [max, values.length].max }).map { |field_index|
      result1.map { |values| values[field_index] }
    }
  else
    fields = ["tag"]
    result = execute(<<-EOS).map { |name, value| [0 < value.length ? "#{name}:#{value}" : name] }
      SELECT DISTINCT tags.name, tags.value FROM hosts_tags
        INNER JOIN tags ON hosts_tags.tag_id = tags.id;
    EOS
  end
  if 0 < result.length
    STDOUT.print(format(result, fields: fields))
    logger.info("found %d tag(s)." % result.length)
  end
end