class HammerCLIForeman::ListCommand

Constants

DEFAULT_PER_PAGE
RETRIEVE_ALL_PER_PAGE

Public Class Methods

command_names(*names) click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 270
def self.command_names(*names)
  super(*names) || %w(list index)
end
output(definition = nil, &block) click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 243
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 252
def adapter
  @context[:adapter] || :table
end
execute() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 274
def execute
  if should_retrieve_all?
    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 266
def extend_data(record)
  record
end
help() click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/commands.rb, line 286
def help
  return super unless resource

  meta = resource.action(action).apidoc&.dig(:metadata)
  if meta && meta[:search] && respond_to?(:option_search)
    self.class.extend_help do |h|
      h.section(_('Search / Order 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 256
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 262
def transform_format(data)
  HammerCLIForeman.collection_to_common_format(data)
end

Protected Instance Methods

current_chunk() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 345
def current_chunk
  return :single unless @meta

  first_chunk = @meta[:page] == 1
  last_chunk = @meta[:per_page] * @meta[:page] >= @meta[:subtotal]
  return :single if first_chunk && last_chunk
  return :first if first_chunk
  return :last if last_chunk

  :another
end
pagination_supported?() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 317
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 304
def retrieve_all
  self.option_per_page = RETRIEVE_ALL_PER_PAGE
  self.option_page = 1

  loop do
    data = send_request
    print_data(data, current_chunk: current_chunk)
    break unless data.size == RETRIEVE_ALL_PER_PAGE

    self.option_page += 1
  end
end
search_field_help_value(field) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 337
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 327
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 321
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