diff --git a/src/cli/cli_helper.rb b/src/cli/cli_helper.rb index 30e7116320..5cdb3f5bbf 100644 --- a/src/cli/cli_helper.rb +++ b/src/cli/cli_helper.rb @@ -14,6 +14,8 @@ # limitations under the License. # #--------------------------------------------------------------------------- # +require 'csv' + module CLIHelper LIST = { :name => "list", @@ -23,6 +25,12 @@ module CLIHelper :description => "Selects columns to display with list command" } + CSV = { + :name => "csv", + :large => "--csv", + :description => "Write table in csv format" + } + #ORDER = { # :name => "order", # :short => "-o x,y,z", @@ -56,7 +64,7 @@ module CLIHelper } #OPTIONS = [LIST, ORDER, FILTER, HEADER, DELAY] - OPTIONS = [LIST, DELAY, FILTER] + OPTIONS = [LIST, DELAY, FILTER, CSV] # Sets bold font def CLIHelper.scr_bold @@ -154,6 +162,8 @@ module CLIHelper class ShowTable require 'yaml' + attr_reader :default_columns + def initialize(conf=nil, ext=nil, &block) @columns = Hash.new @default_columns = Array.new @@ -241,7 +251,7 @@ module CLIHelper private def print_table(data, options) - CLIHelper.print_header(header_str) + CLIHelper.print_header(header_str) if !options[:csv] data ? print_data(data, options) : puts end @@ -257,17 +267,24 @@ module CLIHelper end begin - res_data.each{|l| - puts (0..ncolumns-1).collect{ |i| - dat=l[i] - col=@default_columns[i] + if options[:csv] + CSV($stdout, :write_headers => true, + :headers => @default_columns) do |csv| + res_data.each {|l| csv << l } + end + else + res_data.each{|l| + puts (0..ncolumns-1).collect{ |i| + dat=l[i] + col=@default_columns[i] - str=format_str(col, dat) - str=CLIHelper.color_state(str) if i==stat_column + str=format_str(col, dat) + str=CLIHelper.color_state(str) if i==stat_column - str - }.join(' ').rstrip - } + str + }.join(' ').rstrip + } + end rescue Errno::EPIPE end end