class Gzr::Commands::Space::Top

Public Class Methods

new(options) click to toggle source
Calls superclass method Gzr::Command::new
# File lib/gzr/commands/space/top.rb, line 33
def initialize(options)
  super()
  @options = options
end

Public Instance Methods

execute(input: $stdin, output: $stdout) click to toggle source
# File lib/gzr/commands/space/top.rb, line 38
def execute(input: $stdin, output: $stdout)
  say_warning("options: #{@options.inspect}") if @options[:debug]
  with_session do
    extra_fields = %w(is_shared_root is_users_root is_embed_shared_root is_embed_users_root)
    query_fields = (@options[:fields].split(',') + extra_fields).uniq
    spaces = all_spaces(query_fields.join(','))

    begin
      puts "No spaces found"
      return nil
    end unless spaces && spaces.length > 0

    table_hash = Hash.new
    fields = field_names(@options[:fields])
    table_hash[:header] = fields unless @options[:plain]
    expressions = fields.collect { |fn| field_expression(fn) }
    rows = []
    spaces.each do |h|
      if ( h.is_shared_root || h.is_users_root || h.is_embed_shared_root || h.is_embed_users_root) then
        rows << expressions.collect do |e|
          eval "h.#{e}"
        end
      end
    end
    table_hash[:rows] = rows
    table = TTY::Table.new(table_hash)
    begin
      if @options[:csv] then
        output.puts render_csv(table)
      else
        output.puts table.render(if @options[:plain] then :basic else :ascii end, alignments: [:right])
      end
    end if table
  end
end