Skip to content

Commit

Permalink
Refs #19135 - Possibility to limit fields that are displayed (#407)
Browse files Browse the repository at this point in the history
* Refs #19135 - Possibility to limit fields that are displayed
  • Loading branch information
ofedoren authored and mbacovsky committed Jun 5, 2019
1 parent 943ea36 commit e69943b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/hammer_cli_foreman.rb
Expand Up @@ -22,6 +22,7 @@ def self.exception_handler_class
require 'hammer_cli_foreman/logger'

begin
require 'hammer_cli_foreman/command_extensions'
require 'hammer_cli_foreman/commands'
require 'hammer_cli_foreman/associating_commands'
require 'hammer_cli_foreman/references'
Expand Down
1 change: 1 addition & 0 deletions lib/hammer_cli_foreman/command_extensions.rb
@@ -0,0 +1 @@
require 'hammer_cli_foreman/command_extensions/fields'
13 changes: 13 additions & 0 deletions lib/hammer_cli_foreman/command_extensions/fields.rb
@@ -0,0 +1,13 @@
module HammerCLIForeman
module CommandExtensions
class Fields < HammerCLI::CommandExtensions
inheritable true

use_option :fields

option_sources do |sources, command|
sources << HammerCLIForeman::OptionSources::FieldsParams.new(command)
end
end
end
end
21 changes: 21 additions & 0 deletions lib/hammer_cli_foreman/commands.rb
Expand Up @@ -219,6 +219,15 @@ class ListCommand < Command
RETRIEVE_ALL_PER_PAGE = 1000
DEFAULT_PER_PAGE = 20

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

def adapter
@context[:adapter] || :table
end
Expand Down Expand Up @@ -267,6 +276,8 @@ def help
super
end

extend_with(HammerCLIForeman::CommandExtensions::Fields.new)

protected

def retrieve_all
Expand Down Expand Up @@ -353,6 +364,15 @@ class InfoCommand < SingleResourceCommand

action :show

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

def self.command_name(name=nil)
super(name) || "info"
end
Expand All @@ -370,6 +390,7 @@ def print_data(record)
print_record(output_definition, record)
end

extend_with(HammerCLIForeman::CommandExtensions::Fields.new)
end


Expand Down
1 change: 1 addition & 0 deletions lib/hammer_cli_foreman/option_sources.rb
Expand Up @@ -2,3 +2,4 @@
require 'hammer_cli_foreman/option_sources/ids_params'
require 'hammer_cli_foreman/option_sources/self_param'
require 'hammer_cli_foreman/option_sources/user_params'
require 'hammer_cli_foreman/option_sources/fields_params'
20 changes: 20 additions & 0 deletions lib/hammer_cli_foreman/option_sources/fields_params.rb
@@ -0,0 +1,20 @@
module HammerCLIForeman
module OptionSources
class FieldsParams < HammerCLI::Options::Sources::Base
def initialize(command)
@command = command
end

def process(defined_options, result)
get_options(defined_options, result)
end

def get_options(_defined_options, result)
if @command.respond_to?(:option_fields) && @command.option_fields == ['THIN']
result[HammerCLI.option_accessor_name('thin')] = true
end
result
end
end
end
end

0 comments on commit e69943b

Please sign in to comment.