Skip to content

Commit

Permalink
Fixes #19135 - Possibility to limit fields that are displayed
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Jun 8, 2018
1 parent a3f46d1 commit 371a5fd
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/hammer_cli/main.rb
Expand Up @@ -39,6 +39,9 @@ class MainCommand < AbstractCommand
option ["--interactive"], "INTERACTIVE", _("Explicitly turn interactive mode on/off"),
:format => HammerCLI::Options::Normalizers::Bool.new,
:context_target => :interactive
option ["--only"], "FIELDS", _("Show selected columns or fields only"),
:format => HammerCLI::Options::Normalizers::List.new,
:context_target => :only
option ["--no-headers"], :flag, _("Hide headers from output")
option ["--csv"], :flag, _("Output as CSV (same as --output=csv)")
option ["--output"], "ADAPTER", _("Set output format. One of [%s]") %
Expand Down
25 changes: 24 additions & 1 deletion lib/hammer_cli/output/adapter/tree_structure.rb
Expand Up @@ -7,9 +7,10 @@ def initialize(context={}, formatters={})
end

def prepare_collection(fields, collection)
collection.map do |element|
prepared = collection.map do |element|
render_fields(fields, element)
end
reduce_collection(prepared)
end

protected
Expand Down Expand Up @@ -75,5 +76,27 @@ def numbered_data(data)
end
end

private

def reduce_collection(collection)
return collection unless @context.key?(:only)
reduced = []
collection.each do |record|
reduced << reduce_record(record, @context[:only])
end
reduced
end

def reduce_record(record, keys)
reduced = {}
record.each do |key, value|
if keys.include?(key)
reduced[key] = value
elsif value.is_a? Hash
reduced.merge!(reduce_record(value, keys))
end
end
reduced
end
end
end
8 changes: 8 additions & 0 deletions lib/hammer_cli/output/output.rb
Expand Up @@ -17,13 +17,15 @@ def print_error(msg, details=nil, msg_params={})
end

def print_record(definition, record)
adjust_definition(definition)
adapter.print_record(definition.fields, record)
end

def print_collection(definition, collection)
unless collection.class <= HammerCLI::Output::RecordCollection
collection = HammerCLI::Output::RecordCollection.new([collection].flatten(1))
end
adjust_definition(definition)
adapter.print_collection(definition.fields, collection)
end

Expand Down Expand Up @@ -69,5 +71,11 @@ def init_adapter(adapter_name)
@adapter ||= self.class.adapters[adapter_name].new(context, self.class.formatters)
end

def adjust_definition(definition)
return unless @context.key?(:only)
return if adapter.class <= HammerCLI::Output::Adapter::TreeStructure
labels = @context[:only].map(&:downcase)
definition.select_fields!(deep_select: true, labels: labels)
end
end
end
17 changes: 17 additions & 0 deletions test/unit/output/adapter/base_test.rb
Expand Up @@ -220,6 +220,23 @@

end

context '--only' do
let(:selected_fields) { [name.label] }
let(:context) { { :only => selected_fields, :adapter => :base } }
let(:output) { HammerCLI::Output::Output.new(context) }

it 'should print selected fields only' do
fields = [name, surname, city]
output_definition = HammerCLI::Output::Definition.new
output_definition.fields = fields
expected_output = [
'Name: John',
"\n"
].join("\n")

proc { output.print_collection(output_definition, data) }.must_output(expected_output)
end
end
end

end
14 changes: 14 additions & 0 deletions test/unit/output/adapter/csv_test.rb
Expand Up @@ -227,6 +227,20 @@ def format(data, field_params={})

end

context '--only' do
let(:selected_fields) { [field_name.label] }
let(:context) { {:only => selected_fields, :adapter => :csv } }
let(:output) { HammerCLI::Output::Output.new(context) }

it 'should print selected fields only' do
fields = [field_name, field_started_at]
output_definition = HammerCLI::Output::Definition.new
output_definition.fields = fields
expected_output = "Name\nJohn Doe\n"

proc { output.print_collection(output_definition, data) }.must_output(expected_output)
end
end
end

context "print message" do
Expand Down
12 changes: 12 additions & 0 deletions test/unit/output/adapter/json_test.rb
Expand Up @@ -268,6 +268,18 @@

end

context '--only' do
let(:selected_fields) { [name.label] }
let(:context) { {:only => selected_fields } }

it 'should print selected fields only' do
fields = [name, surname, city]
hash = [{ 'Name' => 'John' }]
expected_output = JSON.pretty_generate(hash) + "\n"

proc { adapter.print_collection(fields, data) }.must_output(expected_output)
end
end
end

end
21 changes: 21 additions & 0 deletions test/unit/output/adapter/table_test.rb
Expand Up @@ -380,6 +380,27 @@ def format(data, field_params={})

end

context '--only' do
let(:selected_fields) { [field_firstname.label] }
let(:context) { {:only => selected_fields, :adapter => :table } }
let(:output) { HammerCLI::Output::Output.new(context) }

it 'should print selected fields only' do
fields = [field_firstname, field_lastname, field_long]
output_definition = HammerCLI::Output::Definition.new
output_definition.fields = fields
expected_output = [
"---------",
"FIRSTNAME",
"---------",
"John ",
"---------",
""
].join("\n")

proc { output.print_collection(output_definition, data) }.must_output(expected_output)
end
end
end

end
12 changes: 12 additions & 0 deletions test/unit/output/adapter/yaml_test.rb
Expand Up @@ -265,6 +265,18 @@

end

context '--only' do
let(:selected_fields) { [name.label] }
let(:context) { { :only => selected_fields } }

it 'should print selected fields only' do
fields = [name, surname, city]
hash = [{ 'Name' => 'John' }]
expected_output = YAML.dump(hash)

proc { adapter.print_collection(fields, data) }.must_output(expected_output)
end
end
end

end

0 comments on commit 371a5fd

Please sign in to comment.