]> gitweb.fluxo.info Git - leap/leap_cli.git/commitdiff
fixed broken `leap list`
authorelijah <elijah@riseup.net>
Sun, 25 Nov 2012 04:22:45 +0000 (20:22 -0800)
committerelijah <elijah@riseup.net>
Sun, 25 Nov 2012 04:22:45 +0000 (20:22 -0800)
bin/leap
leap_cli.gemspec
lib/leap_cli/commands/list.rb
lib/leap_cli/config/manager.rb

index 5912d552a1a71dd30a533eb40db26d8a95eec36f..461ff3c985889c18d7c56b78671f35d16f7e411e 100755 (executable)
--- a/bin/leap
+++ b/bin/leap
@@ -26,7 +26,6 @@ end
 require 'gli'
 require 'highline'
 require 'forwardable'
-require 'terminal-table'
 
 #
 # Typically, GLI and Highline methods are loaded into the global namespace.
@@ -38,7 +37,6 @@ require 'terminal-table'
 module LeapCli::Commands
   extend GLI::App
   extend Forwardable
-  extend Terminal::Table::TableHelper
 
   #
   # delegate highline methods to make them available to sub-commands
index 8b520c8ab3ca3c17419ebcf582d5fdcaf49ab439..1a259fcdb2ddd1c53de26b50f89867547477d7bd 100644 (file)
@@ -47,7 +47,7 @@ spec = Gem::Specification.new do |s|
 
   # console gems
   s.add_runtime_dependency('gli','~> 2.3')
-  s.add_runtime_dependency('terminal-table')
+  s.add_runtime_dependency('command_line_reporter')
   s.add_runtime_dependency('highline')
   s.add_runtime_dependency('paint')
 
index 033c95f3139d14b0b8773d16c45568cbb732e24e..5f455c0771214ae764501bab39d6c399559d1aac 100644 (file)
-module LeapCli
-  module Commands
+require 'command_line_reporter'
 
-    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'])
+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|
+      puts
+      if options['print']
+        print_node_properties(manager.filter(args), options['print'])
+      else
+        if args.any?
+          NodeTable.new(manager.filter(args)).run
         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
+          TagTable.new('SERVICES', manager.services).run
+          TagTable.new('TAGS', manager.tags).run
+          NodeTable.new(manager.nodes).run
         end
       end
     end
+  end
 
-    private
+  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
+  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
+    puts
+  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
+  class TagTable
+    include CommandLineReporter
+    def initialize(heading, tag_list)
+      @heading = heading
+      @tag_list = tag_list
+    end
+    def run
+      tags = @tag_list.keys.sort
+      max_width = [20, (tags+[@heading]).inject(0) {|max,i| [i.size,max].max}].max
+      table :border => false do
+        row :header => true, :color => 'cyan'  do
+          column @heading, :align => 'right', :width => max_width
+          column "NODES", :width => HighLine::SystemExtensions.terminal_size.first - max_width - 2, :padding => 2
         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
+        tags.each do |tag|
+          row do
+            column tag
+            column @tag_list[tag].node_list.keys.sort.join(', ')
           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
+      vertical_spacing
+    end
+  end
+
+  #
+  # might be handy: HighLine::SystemExtensions.terminal_size.first
+  #
+  class NodeTable
+    include CommandLineReporter
+    def initialize(node_list)
+      @node_list = node_list
+    end
+    def run
+      rows = @node_list.keys.sort.collect do |node_name|
+        [node_name, @node_list[node_name].services.sort.join(', '), @node_list[node_name].tags.sort.join(', ')]
+      end
+      unless rows.any?
+        puts Paint["no results", :red]
+        puts
+        return
+      end
+      padding = 2
+      max_node_width    = [20, (rows.map{|i|i[0]} + ["NODES"]   ).inject(0) {|max,i| [i.size,max].max}].max
+      max_service_width = (rows.map{|i|i[1]} + ["SERVICES"]).inject(0) {|max,i| [i.size+padding+padding,max].max}
+      max_tag_width     = (rows.map{|i|i[2]} + ["TAGS"]    ).inject(0) {|max,i| [i.size,max].max}
+      table :border => false do
+        row :header => true, :color => 'cyan'  do
+          column "NODES", :align => 'right', :width => max_node_width
+          column "SERVICES", :width => max_service_width, :padding => 2
+          column "TAGS", :width => max_tag_width
+        end
+        rows.each do |r|
+          row do
+            column r[0]
+            column r[1]
+            column r[2]
           end
         end
-        puts t
       end
+      vertical_spacing
     end
-
   end
-end
+
+end; end
index 3d09e091f62a82f4904b272a4f5e566f949c6435..39dbcd2f10bbe1bda279794979f9ac7ab689c1bb 100644 (file)
@@ -229,6 +229,9 @@ module LeapCli
         end
 
         # inherit from tags
+        if node.vagrant?
+          node['tags'] = (node['tags'] || []).to_a + ['local']
+        end
         if node['tags']
           node['tags'].to_a.sort.each do |node_tag|
             tag = @tags[node_tag]