aboutsummaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/list.rb
blob: a186049137d9a6852e3e0e81b2e6a920442de4d0 (plain)
1
2
3
4
5
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
module LeapCli
  module Commands

    def self.print_config_table(type, config_list)
      style = {:border_x => '-', :border_y => ':', :border_i => '-', :width => 60}

      if type == :services
        t = table do
          self.style = style
          self.headings = ['SERVICE', 'NODES']
          list = config_list.keys.sort
          list.each do |name|
            add_row [name, config_list[name].nodes.keys.join(', ')]
            add_separator unless name == list.last
          end
        end
        puts t
        puts "\n\n"
      elsif type == :tags
        t = table do
          self.style = style
          self.headings = ['TAG', 'NODES']
          list = config_list.keys.sort
          list.each do |name|
            add_row [name, config_list[name].nodes.keys.join(', ')]
            add_separator unless name == list.last
          end
        end
        puts t
        puts "\n\n"
      elsif type == :nodes
        t = table do
          self.style = style
          self.headings = ['NODE', 'SERVICES', 'TAGS']
          list = config_list.keys.sort
          list.each do |name|
            add_row [name, config_list[name].services.to_a.join(', '), config_list[name].tags.to_a.join(', ')]
            add_separator unless name == list.last
          end
        end
        puts t
      end
    end

    desc 'List nodes and their classifications'
    long_desc 'Prints out a listing of nodes, services, or tags.'
    arg_name 'filter'
    command :list do |c|
      c.action do |global_options,options,args|
        if args.any?
          print_config_table(:nodes, ConfigManager.filter(args))
        else
          print_config_table(:services, ConfigManager.services)
          print_config_table(:tags,     ConfigManager.tags)
          print_config_table(:nodes,  ConfigManager.nodes)
        end
      end
    end

  end
end