1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-03-25 02:50:08 +03:00

feature #1484: add csv output to table formater

This commit is contained in:
Javi Fontan 2014-02-18 17:50:08 +01:00
parent 4797588156
commit e49687be51

View File

@ -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