class HammerCLIForeman::ListCommand

Constants

DEFAULT_PER_PAGE
RETRIEVE_ALL_PER_PAGE

Public Class Methods

command_name(name=nil) click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 260
def self.command_name(name=nil)
  super(name) || "list"
end
output(definition = nil, &block) click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 233
def self.output(definition = nil, &block)
  super
  begin
    output_definition.update_field_sets('THIN', (searchables.for(resource).map(&:name) + ['id']).map(&:to_sym))
  rescue StandardError => e
    # Some subcommands may not have such fields or defined resource
  end
end

Public Instance Methods

adapter() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 242
def adapter
  @context[:adapter] || :table
end
execute() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 264
def execute
  if should_retrieve_all?
    print_data(retrieve_all)
  else
    self.option_page = (self.option_page || 1).to_i if respond_to?(:option_page)
    self.option_per_page = (self.option_per_page || HammerCLI::Settings.get(:ui, :per_page) || DEFAULT_PER_PAGE).to_i if respond_to?(:option_per_page)
    print_data(send_request)
  end

  return HammerCLI::EX_OK
end
extend_data(record) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 256
def extend_data(record)
  record
end
help() click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 276
def help
  return super unless resource

  meta = resource.action(action).apidoc[:metadata]
  if meta && meta[:search] && respond_to?(:option_search)
    self.class.extend_help do |h|
      h.section(_('Search fields'), id: :search_fields_section) do |h|
        h.list(search_fields_help(meta[:search]))
      end
    end
  end
  super
end
send_request() click to toggle source
Calls superclass method HammerCLIForeman::Command#send_request
# File lib/hammer_cli_foreman/commands.rb, line 246
def send_request
  set = super
  set.map! { |r| extend_data(r) }
  set
end
transform_format(data) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 252
def transform_format(data)
  HammerCLIForeman.collection_to_common_format(data)
end

Protected Instance Methods

pagination_supported?() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 309
def pagination_supported?
  respond_to?(:option_page) && respond_to?(:option_per_page)
end
retrieve_all() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 294
def retrieve_all
  self.option_per_page = RETRIEVE_ALL_PER_PAGE
  self.option_page = 1

  d = send_request
  all = d

  while (d.size == RETRIEVE_ALL_PER_PAGE) do
    self.option_page += 1
    d = send_request
    all += d
  end
  all
end
search_field_help_value(field) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 329
def search_field_help_value(field)
  if field[:values] && field[:values].is_a?(Array)
    _('Values') + ': ' + field[:values].join(', ')
  else
    field[:type] || field[:values]
  end
end
search_fields_help(search_fields) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 319
def search_fields_help(search_fields)
  return [] if search_fields.nil?

  search_fields.each_with_object([]) do |field, help_list|
    help_list << [
      field[:name], search_field_help_value(field)
    ]
  end
end
should_retrieve_all?() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 313
def should_retrieve_all?
  retrieve_all = pagination_supported? && option_per_page.nil? && option_page.nil?
  retrieve_all &&= HammerCLI::Settings.get(:ui, :per_page).nil? if output.adapter.paginate_by_default?
  retrieve_all
end