summaryrefslogtreecommitdiff
path: root/lib/leap_cli/commands/list.rb
blob: 033c95f3139d14b0b8773d16c45568cbb732e24e (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
module LeapCli
  module Commands

    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.flag 'print', :desc => 'What attributes to print (optional)'
      c.action do |global_options,options,args|
        if options['print']
          print_node_properties(manager.filter(args), options['print'])
        else
          if args.any?
            print_config_table(:nodes, manager.filter(args))
          else
            print_config_table(:services, manager.services)
            print_config_table(:tags, manager.tags)
            print_config_table(:nodes, manager.nodes)
          end
        end
      end
    end

    private

    def self.print_node_properties(nodes, properties)
      node_list = manager.nodes
      properties = properties.split(',')
      max_width = nodes.keys.inject(0) {|max,i| [i.size,max].max}
      nodes.keys.sort.each do |node_name|
        value = properties.collect{|prop| node_list[node_name][prop]}.join(', ')
        printf("%#{max_width}s   %s\n", node_name, value)
      end
    end

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

      if type == :services
        t = table do
          self.style = style
          self.headings = ['SERVICE', 'NODES']
          list = object_list.keys.sort
          list.each do |name|
            add_row [name, object_list[name].node_list.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 = object_list.keys.sort
          list.each do |name|
            add_row [name, object_list[name].node_list.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 = object_list.keys.sort
          list.each do |name|
            services = object_list[name]['services'] || []
            tags     = object_list[name]['tags'] || []
            add_row [name, services.to_a.join(', '), tags.to_a.join(', ')]
            add_separator unless name == list.last
          end
        end
        puts t
      end
    end

  end
end